Maduranga E
Maduranga E

Reputation: 1689

How to disconnect from XMPP on iOS?

I have XMPP installed and it is used for chatting in my iOS app. It should be possible for the user to log out and let a different user to log in and use the application. Currently XMPP chat works well but if I log out and try to log in again with a different username, it gives the following error.

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

I have the following code in my log out method.

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

any ideas on how to log out a user properly and prepare for a new login?

Upvotes: 3

Views: 2030

Answers (2)

Fatima Arshad
Fatima Arshad

Reputation: 350

You can disconnect XMPP this way:

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

Upvotes: 2

chris
chris

Reputation: 95

I don't know if what I did is the best way, but it worked.

To logout I used

- (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];



}

And then to login again:

 - (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];  
}

Hope it helps!

Upvotes: 1

Related Questions