Search code examples
iosibm-mobilefirst

What is the equivalent of iOS applicationWillTerminate in IBM MobileFirst Hybrid app?


Scenario:

I want to call the logout function if the app is terminated. I'm able to do it using native code:

- (void)applicationWillTerminate:(UIApplication *)app
{
    // Run Logout function
}

Problem:

How to do it in IBM mobilefirst hybrid app?

// ************************************************

Edited

First of all, user login in to the app, if the user key in the correct user id and password, it will add the userIdentity into "loginRealm".

WL.Server.setActiveUser("loginRealm", userIdentity);

Next, user closes the apps without logout. So, when the user login for the another time, MFP server will not return any feedback since it will hit this exception:

Cannot change identity of an already logged in user in realm 'loginRealm'. The application must logout first.

Hence, I have to logout the user from MFP server by setting the "loginRealm" to null in adapter;

WL.Server.setActiveUser("loginRealm", null);

The above line of code is in the logout function defined in authentication-config.xml.

The client side device runs this line of code and it will trigger the logout function. Besides, it will reload the App upon success:

WL.Client.logout('loginRealm', {
    onSuccess: WL.Client.reloadApp
});

Steps that I've tried:

1) At WlcommonInit() I added WL.Client.updateUserInfo(); and if WL.Client.isUserAuthenticated("loginRealm") return true I will logout the user from server. However, WL.Client.isUserAuthenticated("loginRealm") will always return false. This is because, it needs to take sometime around (30seconds to 2 minutes) for the flag to turn true after WL.Client.updateUserInfo();. So my login still fail and hit the same error.

2) I tried to logout the users during the user click login button. But the app will refresh and return to login page again due to reloadApp. The logout code I get from IBM mobilefirst website. So user need to click and type 2 times in order to login into the main menu.

WL.Client.logout('loginRealm', {
    onSuccess: WL.Client.reloadApp
});

Am I doing it wrongly? Or are there any other methods to get WL.Client.isUserAuthenticated("loginRealm") return true instantly after WL.Client.updateUserInfo(); ? Can we remove the reload app line of code in logout function?


Solution

  • I don't think this is doable, because that logout function (in MFP) will require server connectivity (request and response) and if the app is by then killed, I think it's going to cause unpredictable results.

    Note though that it seems to be not recommended to use that function anyway? applicationWillTerminate when is it called and when not

    What you should do perhaps in order to simulate it, is to logout-on-login, so that it would appear that the app is logged out when opening it. You can extend the duration of the splash screen so that the end-user will not see that s/he is logged in (in case the session was still alive between the closing and re-opening of the app), until really logged out and then you can display the login screen again or any other required screen.