IOS+openfire 即时通讯

最近看到很多人在琢磨ios即时通讯这块,于是出去好奇,决定动手试试,今天就先从xmpp这个框架来尝试。

 

其实xmpp也就一个协议而已,更多人还是喜欢把它叫做Jabber

 

首先还是提供一下下载地址:

服务端Jabber,这里我用openfire来搭建:http://www.igniterealtime.org/

这里推荐一篇openfire配置的博文:http://www.cnblogs.com/xiaodao/archive/2013/04/05/3000554.html

客户端就是ios的xmppFrame一些库咯,给个github方便:https://github.com/robbiehanson/XMPPFramework

 

xmppFrame里面有demo,在xcode文件下

 

 

第三方类库,第一步还是惯例地导入需要的库文件

 

       

           

懒人喜欢直接上代码,出来吧,代码君!

 

 

[cpp]  view plain copy print ?
 
  1. - (void)setupStream  
  2. {  
  3.     NSAssert(xmppStream == nil, @"Method setupStream invoked multiple times");  
  4.       
  5.     // 初始化XmppStream  
  6.   
  7.     xmppStream = [[XMPPStream alloc] init];  
  8.       
  9.     #if !TARGET_IPHONE_SIMULATOR  
  10.     {  
  11.         // 想要xampp在后台也能运行?  
  12.         //   
  13.         // P.S. - 虚拟机不支持后台  
  14.           
  15.         xmppStream.enableBackgroundingOnSocket = YES;  
  16.     }  
  17.     #endif  
  18.       
  19.     // 初始化 reconnect  
  20.     //   
  21.     // 这东西可以帮你把意外断开的状态连接回去...具体看它的头文件定义  
  22.       
  23.     xmppReconnect = [[XMPPReconnect alloc] init];  
  24.       
  25.     // 初始化 roster  
  26.       
  27.     xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] init];  
  28. //  xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] initWithInMemoryStore];  
  29.       
  30.     xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:xmppRosterStorage];  
  31.       
  32.     xmppRoster.autoFetchRoster = YES;  
  33.     xmppRoster.autoAcceptKnownPresenceSubscriptionRequests = YES;  
  34.       
  35.     // 初始化 vCard support  
  36.       
  37.     xmppvCardStorage = [XMPPvCardCoreDataStorage sharedInstance];  
  38.     xmppvCardTempModule = [[XMPPvCardTempModule alloc] initWithvCardStorage:xmppvCardStorage];  
  39.       
  40.     xmppvCardAvatarModule = [[XMPPvCardAvatarModule alloc] initWithvCardTempModule:xmppvCardTempModule];  
  41.       
  42.     // 初始化 capabilities  
  43.       
  44.       
  45.     xmppCapabilitiesStorage = [XMPPCapabilitiesCoreDataStorage sharedInstance];  
  46.     xmppCapabilities = [[XMPPCapabilities alloc] initWithCapabilitiesStorage:xmppCapabilitiesStorage];  
  47.   
  48.     xmppCapabilities.autoFetchHashedCapabilities = YES;  
  49.     xmppCapabilities.autoFetchNonHashedCapabilities = NO;  
  50.   
  51.     // 激活xmpp的模块  
  52.   
  53.     [xmppReconnect         activate:xmppStream];  
  54.     [xmppRoster            activate:xmppStream];  
  55.     [xmppvCardTempModule   activate:xmppStream];  
  56.     [xmppvCardAvatarModule activate:xmppStream];  
  57.     [xmppCapabilities      activate:xmppStream];  
  58.   
  59.     // 我们可以加添加委托来获取我们感兴趣的东西  
  60.   
  61.     [xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];  
  62.     [xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()];  
  63.   
  64.     // 下面可以替换成自己的域名和端口  
  65.       
  66.     // 如果你没有提供一个地址,JID也是一样可以代替的,JID的格式类似这样"用户名@域名/roster",框架会自动抓取域名作为你的地址  
  67.       
  68.     // 如果没有设置特殊的端口,默认为5222  
  69.       
  70.     [xmppStream setHostName:@"xxxxx"];  
  71.     [xmppStream setHostPort:5222];    
  72.       
  73.   
  74.     //下面这两个根据你自己配置需要来设置  
  75.     allowSelfSignedCertificates = NO;  
  76.     allowSSLHostNameMismatch = NO;  
  77. }  

好了,具体要前期设置的东西都在上面,接下来就是连接上我们的服务器了

 

 

[cpp]  view plain copy print ?
 
  1. - (BOOL)connect  
  2. {  
  3.     if (![xmppStream isDisconnected]) {  
  4.         return YES;  
  5.     }  
  6. <span style="white-space:pre">  </span>//这里前提你需要提供一个交互界面,可以填写jid和password的,然后把它们的值保存到NSUserDefaults  
  7.     NSString *myJID = [[NSUserDefaults standardUserDefaults] stringForKey:kXMPPmyJID];  
  8.     NSString *myPassword = [[NSUserDefaults standardUserDefaults] stringForKey:kXMPPmyPassword];  
  9.       
  10.     if (myJID == nil || myPassword == nil) {  
  11.         return NO;  
  12.     }  
  13.   
  14.     [xmppStream setMyJID:[XMPPJID jidWithString:myJID]];  
  15.     password = myPassword;  
  16.   
  17.     NSError *error = nil;  
  18.     if (![xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error])  
  19.     {  
  20.         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error connecting"   
  21.                                                             message:@"See console for error details."   
  22.                                                            delegate:nil   
  23.                                                   cancelButtonTitle:@"Ok"   
  24.                                                   otherButtonTitles:nil];  
  25.         [alertView show];  
  26.   
  27.         DDLogError(@"Error connecting: %@", error);  
  28.   
  29.         return NO;  
  30.     }  
  31.   
  32.     return YES;  
  33. }  



 

如果一切顺利的话,执行下面的方法

 

 

[cpp]  view plain copy print ?
 
  1. - (void)xmppStreamDidConnect:(XMPPStream *)sender  
  2. {  
  3.     DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);  
  4.       
  5.     isXmppConnected = YES;  
  6.       
  7.     NSError *error = nil;  
  8.       
  9.     if (![[self xmppStream] authenticateWithPassword:password error:&error])  
  10.     {  
  11.         DDLogError(@"Error authenticating: %@", error);  
  12.     }  
  13. }  
  14.   
  15. - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender  
  16. {  
  17.     DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);  
  18.       
  19.     [self goOnline];  
  20. }  
[cpp]  view plain copy print ?
 
  1. - (void)goOnline  
  2. {  
  3.     XMPPPresence *presence = [XMPPPresence presence]; // type="available" is implicit  
  4.       
  5.     [[self xmppStream] sendElement:presence];  
  6. }  

 

 

如果不成功会调用

 

 

[cpp]  view plain copy print ?
 
  1. - (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error  
  2. {  
  3.     DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);  
  4. }  

 

这种情况JID写错了,或者密码错了,好好检查一下

 

发个成功后的界面,普天同庆一下!

 

 

试试从服务端发一条信息给所有客户端

 

装载(http://blog.csdn.net/bsdg123/article/details/8993479)

猜你喜欢

转载自q364035622.iteye.com/blog/1886467