+ Start a Discussion
Manish Kumar 61Manish Kumar 61 

Mobile SDK - iOS Native App - How to Switch user while logging out

Can anyone tell me if it's possible to programmatically switch to another user while logging out of a native iOS app built using Mobile SDK?

I'm currently doing a logout by doing
[SFUserAccountManager sharedInstance].currentUser = ([SFUserAccountManager sharedInstance].allUserAccounts)[0];

and then re-directing back to login screen. I read somewhere that it's possible to do it directly as Mobile SDK supports multiple parallel logins but not sure how to do it.
Best Answer chosen by Manish Kumar 61
Gaurav KheterpalGaurav Kheterpal
Try doing this
NSArray *allAccounts = [SFUserAccountManager sharedInstance].allUserAccounts;
        if ([allAccounts count] > 1) {
            SFDefaultUserManagementViewController *userSwitchVc = [[SFDefaultUserManagementViewController alloc] initWithCompletionBlock:^(SFUserManagementAction action) {
                [self.window.rootViewController dismissViewControllerAnimated:YES completion:NULL];
            [self.window.rootViewController presentViewController:userSwitchVc animated:YES completion:NULL];
        } else {
            if ([allAccounts count] == 1) {
                [SFUserAccountManager sharedInstance].currentUser = ([SFUserAccountManager sharedInstance].allUserAccounts)[0];
            [[SalesforceSDKManager sharedManager] launch];

- (void)handleUserSwitch:(SFUserAccount *)fromUser
                  toUser:(SFUserAccount *)toUser
    [self log:SFLogLevelDebug format:@"SFUserAccountManager changed from user %@ to %@.  Resetting app.",
     fromUser.userName, toUser.userName];
    [self resetViewState:^{
        [self initializeAppViewState];
        [[SalesforceSDKManager sharedManager] launch];
This will allow you to switch users without explicitly logging them out and logging them in again. The key here is to first check if you have more than one user logged into app.

If there is only one user, you can simply logout. If there are multiple users, you don't logout, instead you use
 to switch to the other user.

I hope this helps.

If my answer helps resolve your query, please mark it as the 'Best Answer' to benefit others and improve the overall quality of Discussion Forums.

Gaurav Kheterpal
Certified Force.com Developer| Developer Forums Moderator| Dreamforce Speaker