XMPP客戶端開發
發表時間:2020-10-19
發布人:葵宇科技
浏覽次數:38
這篇記錄下(xià)我應用XMPP實現上傳頭像的過程,發明XMPP真心很強大年夜很多功能都已經實現了。我們隻要直接調用就可(kě)以了。下(xià)面上代碼了。
惹人頭文(wén)件 這個(gè)頭文(wén)件須要本身導入不然 XMPPvCardTemp 下(xià)的辦法調用不了。
#import "XMPPvCardTemp.h"
聲明相幹對象
// 聲明上傳頭像相幹對象 @property (nonatomic , strong) XMPPvCardCoreDataStorage *xmppvCardStorage; @property (nonatomic , strong) XMPPvCardTempModule *xmppvCardTempModule; @property (nonatomic , strong) XMPPvCardAvatarModule *xmppvCardAvatarModule;
初始化相幹對象
// 初始化辦法 - (void)setupStream { _xmppStream = [[XMPPStream alloc] init]; [_xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()]; _xmppRosterDataStorage = [[XMPPRosterCoreDataStorage alloc] init]; _xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:_xmppRosterDataStorage]; // _xmppRoster.autoFetchRoster = YES; // _xmppRoster.autoAcceptKnownPresenceSubscriptionRequests = YES; // _xmppvCardStorage = [XMPPvCardCoreDataStorage sharedInstance]; _xmppvCardTempModule = [[XMPPvCardTempModule alloc] initWithvCardStorage:_xmppvCardStorage]; _xmppvCardAvatarModule = [[XMPPvCardAvatarModule alloc] initWithvCardTempModule:_xmppvCardTempModule]; [_xmppvCardTempModule addDelegate:self delegateQueue:dispatch_get_main_queue()]; [_xmppvCardAvatarModule addDelegate:self delegateQueue:dispatch_get_main_queue()]; }
在登錄成功的回調中(zhōng)與XMPPStream對象進行接洽關(guān)系
// 上岸成功 - (void) xmppStreamDidAuthenticate:(XMPPStream *)sender { [self goOnline]; NSLog(@"上岸成功"); [[NSUserDefaults standardUserDefaults] setObject:@"success" forKey:XMPPLogin]; [_xmppRoster activate:_xmppStream]; [_xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()]; [_xmppvCardTempModule activate:_xmppStream]; [_xmppvCardAvatarModule activate:_xmppStream]; [self queryRoster]; }
上傳頭像的代碼
原文(wén)地址:http://blog.csdn.net/qqmcy/article/details/42422893
- (void) offeringProfile { NSXMLElement *vCardXML = [NSXMLElement elementWithName:@"vCard" stringValue:@"vcard-temp"]; NSXMLElement *photoXML = [NSXMLElement elementWithName:@"PHOTO"]; NSXMLElement *typeXML = [NSXMLElement elementWithName:@"TYPE" stringValue:@"image/jpeg"]; UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"changmen" ofType:@"jpg">; NSData *dataFromImage = UIImageJPEGRepresentation(image, 0.7f); NSXMLElement *binvalXML = [NSXMLElement elementWithName:@"BINVAL" stringValue:[dataFromImage base64Encoding]]; [photoXML addChild:typeXML]; [photoXML addChild:binvalXML]; [photoXML addChild:photoXML]; XMPPvCardTemp * myvCardTemp = [_xmppvCardTempModule myvCardTemp]; NSLog(@"%@",myvCardTemp); if (myvCardTemp) { myvCardTemp.photo = dataFromImage; [_xmppvCardTempModule updateMyvCardTemp:myvCardTemp]; }else { XMPPvCardTemp *newvCardTemp = [XMPPvCardTemp vCardTempFromElement:vCardXML]; newvCardTemp.nickname = @"nick"; [_xmppvCardTempModule updateMyvCardTemp:newvCardTemp]; } }