iPhone平台下基于XMPP的IM研究


看了下iphone平台下xmpp的使用。XmppFramework 是一个开源项目,使用Objective-C实现了XMPP协议,它和前面所说的smack使用起来一样的方便,不过官网上提供的资料远不及smack。

源码地址:http://code.google.com/p/xmppframework/,目前需要使用git才能download到源码,。

PC客户端使用Spark,不知是否是我的黑苹果原因,spark装上不能运行(郁闷中...)

服务器使用Openfire

数据库我使用还是MySQL

怎样将XMPPFramework添加到我们自己的项目中,请参考。

代码步骤:

1、初始化XMPPStream

xmppStream = [[XMPPStream alloc] init];

xmppStream.hostName = @"127.0.0.1";

xmppStream.hostPort = 5222;

[xmppStreamaddDelegate:selfdelegateQueue:dispatch_get_main_queue()];

XmppFramework的消息监听方式使用delegate。在smack中我们使用的是listener,其实都一样。

2、设置JID;(注意JID的Domain一定要用主机名,不要用IP地址。我的疏忽让我晚上熬到了3点多)

xmppStream.myJID = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@liu-lavymatoMacBook-Pro.local",myJID]];

3、连接服务器

NSError *error = nil;

[xmppStream connect:&error];

接下来就是一系列依次调用delegate的方法

xmppStreamWillConnect

socketDidConnect

xmppStreamDidConnect 在这个方法中我们需要调用:         [xmppStreamauthenticateWithPassword:myPassworderror:&error]

验证成功:xmppStreamDidAuthenticate:

验证失败:xmppStream: didNotAuthenticate:

  1. //   
  2. //  XmppTest1AppDelegate.h   
  3. //  XmppTest1   
  4. //   
  5. //  Created by liu lavy on 11-10-2.   
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.   
  7. //   
  8.   
  9. #import <UIKit/UIKit.h>   
  10. #import "XMPPFramework.h"   
  11.   
  12. @class XmppTest1ViewController;  
  13.   
  14. @interface XmppTest1AppDelegate : NSObject <UIApplicationDelegate, XMPPRosterDelegate> {  
  15.     UIWindow *window;  
  16.     XmppTest1ViewController *viewController;  
  17.     XMPPStream *xmppStream;  
  18.     XMPPReconnect *xmppReconnect;  
  19.     NSString *myPassword;  
  20. }  
  21.   
  22. @property (nonatomic, retain) IBOutlet UIWindow *window;  
  23. @property (nonatomic, retain) IBOutlet XmppTest1ViewController *viewController;  
  24.   
  25. @property (nonatomic, retain) XMPPStream *xmppStream;  
  26. @property (nonatomic, readonly) XMPPReconnect *xmppReconnect;  
  27. @property (nonatomic, retain) NSString *myPassword;  
  28.   
  29.   
  30. -(BOOL) connect:(NSString *)myJID password:(NSString *)myPassword;  
  31. -(BOOL) authenticate;  
  32. -(void) disConnect;  
  33. @end  
  • 1
  • 2
  • 3
  • 下一页

相关内容