3. XMPP用户登录

在appDelegate处理来自XMPP服务器的所有请求

1.在appDelegate.h 中添加XMPP的主头文件

定义XMPP相关的属性的方法定义> 全局的XMPPStream,只读属性

2.在appDelegate.m 的interface中新建方法:遵守XMPPStreamDelegate协议可以方便编写代码(不写也可以)

  1.设置XMPPStream>setupStream

  2.通知服务器用户上线>goOnline

  3.通知服务器用户下线>goOffline

  4.连接到服务器>connect

  5.与服务器断开连接>disConnect

 

 

3.在下面实现方法

//设置XMPPStream

setupStream{

//避免_xmppStream被重复定义,得判断是否为nil,等于nil才实例化

  1.实例化流

  _xmppStream = [[XMPPStream alloc] init];

  2.添加代理。因为所有网络请求都是做基于网络的数据处理,跟 UI无关,因此可以让代理yyifd其化线程中 ,从而提高程序的运行性能

  [_xmppStream addDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)];

}

//连接

connnect{

//1.设置流

[self setupStream];

//2.指定用户名、密码、 主机teacher.local

比如:  NSString *userName =@"lisi@teacher.local";

//3.设置流的JID和主机

  [_xmppStream setMyJID:[XMPPJID jidWithString:userName]];

  [_xmppStream setHostName:主机名];

//4.开始连接

  if(error){

  // 连接请求出错  错误原因可以调用 这个方法: error.localizedDescription

  }else{

  //连接请求成功

  }

 

}

// 设置代理方法   连接完成 (如果服务器地址不对,就不会调用此方法)

- (void)xmppStreamDidConnect:(XMPPStream *)sender{

  // 连接建立

  // 开始身份验证

    //定义密码password

  [_xmppStream authenticateWithPassword:password  error:nil];

}

//身份验证通过

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender{

}

 

//身份验证失败

- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error{

}

//通知服务器用户上线

goOnline{

  //1。实例化一个上线报告

  XMPPPresence *presence = [XMPPPressence presence];

  [_xmppStream sendElement:presence];  //这个方法没有回调,只通知我的好友

}

goOffline{

  //1.实例化一个下线报告

    XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];

  //2.发送presence给服务器,通知服务器客户端下线

    [_xmppStream sendElement:presence];

}

//断开连接

desConnect{

  //通知服务器下线

  [self goOffline];

  //XMPPStream断开连接

  [_xmppStream disconnect];

}

- (void)applicationWillResignActive:(UIApplication *)application {

 // 这是重新恢复

  [self disConnect];

}

- (void)applicationDidBecomeActive:(UIApplication *)application {

   [self connect];  //连接

}

 

 

 

 

appDelegate.m的自带的方法  

- (void)applicationWillResignActive:(UIApplication *)application {

 // 这是重新恢复

}

 

- (void)applicationDidEnterBackground:(UIApplication *)application {

//  这个是退出到后台

  }

 

- (void)applicationWillEnterForeground:(UIApplication *)application {

// 这个方法是记录前台的。

  }

 

- (void)applicationDidBecomeActive:(UIApplication *)application {

   }

 

posted @ 2015-01-24 10:28  对号入座  阅读(207)  评论(0)    收藏  举报