XMPP
是干嘛的在此就不多探討了,我也是剛開始嘗試著學(xué)習(xí)绍赛。首先我們把
XMPP
拉進(jìn)工程蔓纠,然后必須導(dǎo)入的幾個(gè)框架:CFNetwork.framework
Security.framework
libxml2.dylib
libresolv.dylib
SystemConfiguration.framework
CoreLocation.framework
AudioToolbox.framework
AVFoundation.framework
運(yùn)行下不報(bào)錯(cuò)就對(duì)了。吗蚌。腿倚。。蚯妇。
在我們開發(fā)中做聊天的話敷燎,我們首先是需要登陸,登陸之后獲取用戶的信息箩言,
獲取到信息之后我們判斷下該用戶是否已經(jīng)在
Openfire
存在硬贯,不存在就直接給用戶注冊(cè),存在就自動(dòng)賦值登陸Openfire
[self XmppLogin:dic];
#pragma mark 登陸xmpp
-(void)XmppLogin:(NSDictionary*)userdic{
App.IM_Account = [NSString stringWithFormat:@"Bison%@",App.UserId];//拼上前綴
App.IM_Password = @"**************";
[[JEXMPP Shared] XmppLogin];
}
#pragma mark - 登錄 退出
-(BOOL)XmppLogin{
if (![XMPP_Stream isDisconnected]) {
return YES;
}
[XMPP_Stream setHostName:kIM_Host];//設(shè)置服務(wù)器
[XMPP_Stream setHostPort:[kIM_Port intValue]];//設(shè)置端口
NSLog(@"jid:%@ ps:%@",App.IM_Account,App.IM_Password);
if (App.IM_Account == nil || App.IM_Password == nil) {
return NO;
}
XMPPJID *myjid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@%@",App.IM_Account,kIM_Domain] resource:@"jp"];//設(shè)置了 jid 的 resource 即限制了多點(diǎn)登陸
NSError *error ;
[XMPP_Stream setMyJID:myjid];
if (![XMPP_Stream connectWithTimeout:XMPPStreamTimeoutNone error:&error]) {
NSLog(@"my connected error : %@",error.description);
return NO;
}
return YES;
}
接下來(lái)就是驗(yàn)證是否登陸了Openfire
//將要連上
- (void)xmppStreamWillConnect:(XMPPStream *)sender{
NSLog(@"==xmppStreamWillConnect");
}
//連上了 還要驗(yàn)證密碼
- (void)xmppStreamDidConnect:(XMPPStream *)sender{
NSLog(@"xmppStreamDidConnect");
if (App.IM_Password) {
NSError *error ;
if (![XMPP_Stream authenticateWithPassword:App.IM_Password error:&error]) {
NSLog(@"error authenticate : %@",error.description);
}
}
}
//突然掉線了
- (void)xmppReconnect:(XMPPReconnect *)sender didDetectAccidentalDisconnect:(SCNetworkReachabilityFlags)reachabilityFlags {
NSLog(@"didDetectAccidentalDisconnect : \n %u ", reachabilityFlags);
}
//YES 重連
- (BOOL)xmppReconnect:(XMPPReconnect *)sender shouldAttemptAutoReconnect:(SCNetworkReachabilityFlags)reachabilityFlags{
NSLog(@"shouldAttemptAutoReconnect : \n %u ", reachabilityFlags);
return YES;
}
#pragma mark 發(fā)送 成功 失敗
- (void)xmppStream:(XMPPStream *)sender didSendMessage:(XMPPMessage *)message{
}
//發(fā)送失敗
- (void)xmppStream:(XMPPStream *)sender didFailToSendMessage:(XMPPMessage *)message error:(NSError *)error{
}
//發(fā)送 成功或失敗的 反饋給聊天頁(yè)面
-(void)XmppSend_Status:(NSInteger)status Msg:(JEChatMessage *)msg{
NSLog(@"XmppSend_Status 消息發(fā)送:%@ \n內(nèi)容:%@", status == 1 ? @"成功" : @"失敗",msg.TextContent);
}
#pragma mark 接收消息
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{
NSLog(@"%@\n 來(lái)新消息了\n from:%@\n to:%@\n type:%@\n message:%@\n body:%@",message,[[message attributeForName:@"from"] stringValue],[[message attributeForName:@"to"] stringValue],[[message attributeForName:@"type"] stringValue],[[message attributeForName:@"message"] stringValue],[[message elementForName:@"body"] stringValue]);
}
推薦一款學(xué)習(xí)iOS開發(fā)的app_____|______| | 傳送門
今天暫時(shí)講到這陨收。饭豹。。务漩。拄衰。
有不足之處希望大家指點(diǎn)出來(lái)哦,因?yàn)槲乙矊?duì)xmpp剛接觸不久
技術(shù)交流群:534926022(免費(fèi)) 511040024(0.8/人付費(fèi))
原文在:http://www.allluckly.cn/
版權(quán)歸?Bison所有 如需轉(zhuǎn)載請(qǐng)保留原文超鏈接地址饵骨!否則后果自負(fù)翘悉!