iOS XMPPFramework不发送状态

时间:2014-07-31 08:17:18

标签: ios objective-c xmpp xmppframework

我的xmpp流成功连接,在回调中我尝试发送用户的存在。但是,我不断收到此错误:Error Domain = XMPPStreamErrorDomain Code = 1“无法完成操作。(XMPPStreamErrorDomain错误1。)”

我的连接方法:

- (void)connect {
    NSString *username = @"masa060295@jabber.web.id";
    self.password = @"test123";

    [self.xmppStream setMyJID:[XMPPJID jidWithString:username]];

    NSError *error = nil;
    if (![self.xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error])
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:[NSString stringWithFormat:@"Can't connect to server %@", [error localizedDescription]]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                    otherButtonTitles:nil];
        [alertView show];
    }
}

我的回调:

- (void)xmppStreamDidConnect:(XMPPStream *)sender {
    NSError *error = nil;

    NSLog(@"%hhd", [self.xmppStream isConnected]);

    if (![self.xmppStream authenticateWithPassword:self.password error:&error]) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:[NSString stringWithFormat:@"Can't authenticate %@", [error localizedDescription]]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
    }

    XMPPPresence *presence = [XMPPPresence presence];
    NSXMLElement *priority = [NSXMLElement elementWithName:@"priority" numberValue:[NSNumber numberWithInt:127]];
    [presence addChild:priority];

    [self.xmppStream sendElement:presence];
}

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您应该在身份验证完成后发送状态。在此回调中执行此操作:

 - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
 {        
      XMPPPresence *presence = [XMPPPresence presence]; // type="available" is implicit

      [sender sendElement:presence];
 }