歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> iPhone平台下基於XMPP的IM研究

iPhone平台下基於XMPP的IM研究

日期:2017/3/1 11:11:03   编辑:Linux編程

看了下iphone平台下xmpp的使用。XmppFramework 是一個開源項目,使用Objective-C實現了XMPP協議,它和前面所說的smack使用起來一樣的方便,不過官網上提供的資料遠不及smack。

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

PC客戶端使用Spark,不知是否是我的黑蘋果原因,spark裝上不能運行(郁悶中...)

服務器使用Openfire

數據庫我使用還是MySQL

怎樣將XMPPFramework添加到我們自己的項目中,請參考http://www.linuxidc.com/Linux/2011-10/45823.htm。

代碼步驟:

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. #import <UIKit/UIKit.h>
  9. #import "XMPPFramework.h"
  10. @class XmppTest1ViewController;
  11. @interface XmppTest1AppDelegate : NSObject <UIApplicationDelegate, XMPPRosterDelegate> {
  12. UIWindow *window;
  13. XmppTest1ViewController *viewController;
  14. XMPPStream *xmppStream;
  15. XMPPReconnect *xmppReconnect;
  16. NSString *myPassword;
  17. }
  18. @property (nonatomic, retain) IBOutlet UIWindow *window;
  19. @property (nonatomic, retain) IBOutlet XmppTest1ViewController *viewController;
  20. @property (nonatomic, retain) XMPPStream *xmppStream;
  21. @property (nonatomic, readonly) XMPPReconnect *xmppReconnect;
  22. @property (nonatomic, retain) NSString *myPassword;
  23. -(BOOL) connect:(NSString *)myJID password:(NSString *)myPassword;
  24. -(BOOL) authenticate;
  25. -(void) disConnect;
  26. @end
Copyright © Linux教程網 All Rights Reserved