XMPP客户端开发
发表时间:2020-10-19
发布人:葵宇科技
浏览次数:44
这篇记录下我应用XMPP实现上传头像的过程,发明XMPP真心很强大年夜很多功能都已经实现了。我们只要直接调用就可以了。下面上代码了。
惹人头文件 这个头文件须要本身导入不然 XMPPvCardTemp 下的办法调用不了。
#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()]; }
在登录成功的回调中与XMPPStream对象进行接洽关系
// 上岸成功 - (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]; }
上传头像的代码
原文地址: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]; } }