XMPP登陆demo Xcode7环境

来源:互联网 发布:阿部力网络主持 编辑:程序博客网 时间:2024/06/05 00:51

在做写代码前必须做的操作

  • 配置服务器(我自己使用Openfire)
  • 导入XMPP框架
  • 添加框架依赖
    完成以上三步操作并且编译通知之后就可以开始写代码了。配置服务器这么简单的操作就不在这里一一讲解,如果不会就自己google看看吧。

导入XMPP框架(这里使用的是3.6.4):

导入Vendor文件夹下面的

  • CocoaLumberjack:日志框架
  • CocoaAsyncSocket:底层网络框架,实现异步Socket网络通讯
  • KissXML:XML解析框架
    需要添加libxml2.dylib框架依赖
    需要指定如下编译选项:
    Header Search Paths = /usr/include/libxml2
    2

    Other Linker Flags = -lxml2
    1

  • libidn

导入下面四个文件夹

  • Authentication
  • Categories
  • Core
  • Utilities
    最后添加libxml2.tbd依赖
    3

全部导入完成编译的时候发现还是会报错,错误信息如下:
4
解决方案如下:
5
再运行,此时代码就没有报错了,可以开始写代码了。

登陆流程
1. 初始化XMPPStream
2. 连接到服务器[传一个JID]
3. 连接到服务成功后,再发送密码授权
4. 授权成功后,发送”在线” 消息
这里为了方便演示就直接写在了AppDelegate里面了。

#import "AppDelegate.h"#import "XMPPFramework.h"/* * 在AppDelegate实现登录 1. 初始化XMPPStream 2. 连接到服务器[传一个JID] 3. 连接到服务成功后,再发送密码授权 4. 授权成功后,发送"在线" 消息 */@interface AppDelegate ()<XMPPStreamDelegate>@property (nonatomic, strong) XMPPStream *xmppStream ;@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    [self connectToHost];    return YES;}#pragma mark - 私有方法//1. 初始化XMPPStream -(XMPPStream *)xmppStream{      if(_xmppStream == nil){          self.xmppStream = [[XMPPStream alloc] init];          // 设置代理          [_xmppStream addDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];      }    return _xmppStream;}//2. 连接到服务器[传一个JID]-(void) connectToHost{    NSLog(@"连接到服务器");    XMPPJID *myJID = [XMPPJID jidWithUser:@"jason" domain:@"jason.local" resource:@"iphone" ];    self.xmppStream.myJID = myJID;    // 设置服务器域名    self.xmppStream.hostName = @"jason.local";//不仅可以是域名,还可是IP地址    // 设置端口 如果服务器端口是5222,可以省略    self.xmppStream.hostPort = 5222;    // 连接    NSError *err = nil;    if(![self.xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&err]){        NSLog(@"错误信息---%@",err);    }}//3. 连接到服务成功后,再发送密码授权-(void) sendPwdToHost{    NSLog(@"发送密码授权");    NSError *err = nil;    [_xmppStream authenticateWithPassword:@"123456" error:&err];    if (err) {        NSLog(@"%@",err);    }}//4. 授权成功后,发送"在线" 消息-(void) sendOnlineToHost{    NSLog(@"发送在线消息");    XMPPPresence *presence = [XMPPPresence presence];    NSLog(@"%@",presence);    [self.xmppStream sendElement:presence];}#pragma mark - XMPPStream代理-(void)xmppStreamDidConnect:(XMPPStream *)sender{    NSLog(@"与主机连接成功");    // 主机连接成功后,发送密码进行授权    [self sendPwdToHost];}-(void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error{    NSLog(@"与主机断开连接 %@",error);}-(void)xmppStreamDidAuthenticate:(XMPPStream *)sender{   NSLog(@"授权成功");    [self sendOnlineToHost];}-(void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error{     NSLog(@"授权失败---%@",error);}#pragma mark - 公共方法-(void)logout{    NSLog(@"退出账号");    XMPPPresence *offline = [XMPPPresence presenceWithType:@"unavailable"];    [self.xmppStream sendElement:offline];    //断开连接    [self.xmppStream disconnect];}@end

登陆运行结果:
6
退出登陆:

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{      AppDelegate *app =[UIApplication sharedApplication].delegate ;      [app logout];}

运行结果:
7
可以看到服务器上 用户名为jason的用户刚刚的绿色图标已经变成白色了,说明该用户已经下线了。
以上代码已经是全部的代码。
项目下载(免积分)

0 0
原创粉丝点击