当前位置:首页 > 操作系统 > Ios

iOS XMPP之常见错误一:(<failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/></failure>)

)' />

在XMPP开发中,使用XMPPStream进行连接服务器后,验证过程中,比较常见的一个错误是

<failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/></failure>.

尤其作为初学者(笔者就是这样的),经常会因为这个问题浪费不少时间。

xmpp的使用

1、创建xmppStream

 

  1. - ( void )setupXmppStream  
  2. {  
  3.     // 1. 实例化  
  4.     _xmppStream = [[XMPPStream alloc] init];  
  5.     [_xmppStream addDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];  
  6. }  

 

2、连接服务器

 

 

  1. - ( void )connect  
  2. {  
  3.     // 1. 实例化XMPPStream  
  4.     [self setupXmppStream];  
  5.       
  6.     NSString *hostName = @"sky.local";  
  7. //    NSString *hostName = @"127.0.0.1";   
  8.     NSString *userName = @"zhaoliu";  
  9.       
  10.     // 设置XMPPStream的hostName&JID  
  11.     _xmppStream.hostName = hostName;  
  12.     _xmppStream.myJID = [XMPPJID jidWithUser:userName domain:hostName resource:nil];  
  13.     // 连接  
  14.     NSError *error = nil;  
  15.     if (![_xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]) {  
  16.         NSLog(@"%@", error.localizedDescription);  
  17.     } else {  
  18.         NSLog(@"发送连接请求成功");  
  19.     }  
  20. }  


3、得到服务器相应后回调方法

 

 

  1. #pragma mark - XMPPStream协议代理方法   
  2. #pragma mark 完成连接   
  3. - (void)xmppStreamDidConnect:(XMPPStream *)sender  
  4. {  
  5.     NSLog(@"success userName = %@, myJID = %@",sender.myJID.user, sender.myJID);  
  6.       
  7.     // 登录到服务器,将用户密码发送到服务器验证身份  
  8.     NSString *password = @"123";  
  9.     [_xmppStream authenticateWithPassword:password error:nil];  
  10. }  
  11.   
  12. #pragma mark 断开连接   
  13. - (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error  
  14. {  
  15.     NSLog(@"断开连接");  
  16. }  


4、登陆信息回调方法(即:[_xmppStream authenticateWithPassword:password error:nil]利用此方法发送密码到服务器完成登陆)

 

 

  1. #pragma mark 身份验证成功   
  2. - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender  
  3. {  
  4.     NSLog(@"身份验证成功!");  
  5. }  
  6.   
  7. #pragma mark 用户名或者密码错误   
  8. - (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error  
  9. {  
  10.     NSLog(@"用户名或者密码错误 error = %@",error);  
  11. }  


此时如果代码中的domain与openfire服务器中的设置不一样时,就会导致此错误,虽然我们用一些第三方客户端时一样能登陆成功

 

 

  1. NSString  *hostName =  @"sky.local" ;  
  2. NSString  *hostName =  @"127.0.0.1" ;  

当我用adium、spark等第三方软件时,上面的两个都可以登陆成功。但是用代码时,则第一个就会<failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/></failure>报错。提示没有注册、从登陆成功。原因是第一个设置与我opnefire服务器中的设置不一样导致。

 

下面看一下我openfire中的设置

)' src="/upload/getfiles/default/2022/11/14/20221114052333141.jpg" width="668" height="408" />

 

http://blog.csdn.net/sky_2016/article/details/40278661

)' ref='nofollow'>iOS XMPP之常见错误一:( )

原文:http://www.cnblogs.com/yangmx/p/4148923.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!