如何在iOS上断开与XMPP的连接?

时间:2012-11-22 08:43:54

标签: ios xmpp logout

我安装了XMPP,用于在我的iOS应用中聊天。用户应该可以注销并让不同的用户登录并使用该应用程序。目前XMPP聊天工作正常但如果我注销并尝试使用其他用户名再次登录,则会出现以下错误。

2012-11-22 14:15:52.520 FMB[3297:c07] *** Assertion failure in -[AppDelegate setupStream], /Visni/Project/FMBXMPP/FMB/AppDelegate.m:843
2012-11-22 14:15:52.541 FMB[3297:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Method setupStream invoked multiple times'
*** First throw call stack:
(0x24ee012 0x1fe3e7e 0x24ede78 0x1a79f35 0x6b46 0x8f49 0x50938 0x2f4a3 0x2ea84 0x215653f 0x2168014 0x21587d5 0x2494af5 0x2493f44 0x2493e1b 0x2b1b7e3 0x2b1b668 0xf2b65c 0x288a 0x2795)
libc++abi.dylib: terminate called throwing an exception

我在退出方法中有以下代码。

AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
        [appDelegate disconnect];

有关如何正确注销用户并准备新登录的任何想法?

2 个答案:

答案 0 :(得分:1)

我不知道我所做的是最好的方式,但它有效。

注销我用过

- (void)resetField:(NSString *)field forKey:(NSString *)key
{

    [[NSUserDefaults standardUserDefaults] setObject:field forKey:key];

}

- (IBAction)logout:(id)sender {

[self resetField:@"" forKey:kXMPPmyJID];
[self resetField:@"" forKey:kXMPPmyPassword];

[[[self appDelegate] xmppStream ]disconnect];
[[[self appDelegate] xmppvCardTempModule] removeDelegate:self];



}

然后再次登录:

 - (void)setField:(UITextField *)field forKey:(NSString *)key {
if (field.text != nil)
{
    [[NSUserDefaults standardUserDefaults] setObject:field.text forKey:key];
} else {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
} 
 }

- (IBAction)login:(id)sender {

[self setField:jidField forKey:kXMPPmyJID];
[self setField:passwordField forKey:kXMPPmyPassword];
[[self appDelegate] connect];  
}

希望它有所帮助!

答案 1 :(得分:1)

您可以通过以下方式断开XMPP:

- (void) disconnectXMPP
{
    [self.xmppStream removeDelegate:self];
    [xmppReconnect         deactivate];
    [self.xmppStream disconnect];
    self.xmppStream = nil;
}