Strategy ONE
Add Custom Logic During the Authentication Process in iOS
The MicroStrategy Mobile SDK authentication APIs let you insert custom logic at different points during the authentication process. You do this by overriding and implementing the methods in the classes that are part of the authentication process.
One of the most common customizations is to add custom logic during project authentication. The following steps and code snippets illustrate how to do this.
-
Create a custom app delegate as shown in Adding Functionality with a custom Application Delegate.
-
Create a new Objective-C class that is a subclass of MSIAuthenticationPromptViewController and add it to the desired targets. The following code snippet shows a sample header file for this class.
// // CustomLoginPromptViewController.h // MicroStrategyMobile // // Copyright (c) 2014 MicroStrategy Inc. All rights reserved. // #import <MicroStrategyMobileSDK/MSIAuthenticationPromptViewController.h> @interface CustomLoginPromptViewController: MSIAuthenticationPromptViewController @end
-
Override the - (void) setupWithMobileLoginPromptView:(MSIMobileLoginPromptView *) mobileLoginPromptView mobileLoginModule:(MSIMobileLoginModule*) mobileLoginModule parameters:(NSDictionary*) promptParameters method to add custom behavior to the view controller if needed. Make sure that this implementation calls its parent implementation before adding custom logic. A sample snippet implementation is shown below:
- (void) setupWithMobileLoginPromptView:(MSIMobileLoginPromptView *) mobileLoginPromptView mobileLoginModule:(MSIMobileLoginModule*) mobileLoginModule parameters:(NSDictionary*) promptParameters{ [super setupWithMobileLoginPromptView:mobileLoginPromptView mobileLoginModule:mobileLoginModule parameters:promptParameters]; //add custom view controller logic if needed
-
Override the different delegate methods implemented by the parent class. A sample code snippet below shows a full implementation file of the custom class. The customization implemented by this file forces a programmatic logout five minutes after the user successfully signs in.
// // CustomLoginPromptViewController.m // MicroStrategyMobile // // Copyright (c) 2014 MicroStrategy Inc. All rights reserved. // #import "CustomLoginPromptViewController.h" #import <MicroStrategyMobileSDK/MSIAuthenticationModule.h> #import <MicroStrategyMobileSDK/MSIProjectInfo.h> @interface CustomLoginPromptViewController () @end @implementation CustomLoginPromptViewController - (void) setupWithMobileLoginPromptView:(MSIMobileLoginPromptView *) mobileLoginPromptView mobileLoginModule:(MSIMobileLoginModule*) mobileLoginModule parameters:(NSDictionary*) promptParameters{ [super setupWithMobileLoginPromptView:mobileLoginPromptView mobileLoginModule:mobileLoginModule parameters:promptParameters]; //add custom view controller logic if needed self.persistProjectAuthenticationCredentialsInPreferencesStore = NO; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* Hook to add custom logic when the view notifies the view controller that the user provided the credentials, one example use case would be to replace the credentials provided dynamically or adding a dynamic token if needed. */ - (void) loginPromptView:(MSIMobileLoginPromptView *)promptView didInputAuthenticationParameters:(NSDictionary*)parameters{ [super loginPromptView:promptView didInputAuthenticationParameters:parameters]; } /* Hook to add custom logic when the authentication process succeeded */ - (void) mobileLoginModule:(MSIMobileLoginModule *)loginModule mobileLoginDidSucceed:(NSDictionary *)successInfo{ [super mobileLoginModule:loginModule mobileLoginDidSucceed:successInfo]; } /** Called when mobile login module failed to login. @param loginModule the mobile login module that sends the message @param _failureInfo The information mobile login module returns to MSIMobileLoginModuleDelegate */ - (void) mobileLoginModule:(MSIMobileLoginModule *)loginModule mobileLoginDidFail:(NSDictionary *)failureInfo{ [super mobileLoginModule:loginModule mobileLoginDidFail:failureInfo]; } /** Called when mobile login module starts login. @param loginModule the mobile login module that sends the message @param _preLoginInfo The information mobile login module returns to MSIMobileLoginModuleDelegate */ - (void) mobileLoginModule:(MSIMobileLoginModule *)loginModule willHandlePreMobileLogin:(NSDictionary *)preLoginInfo{ [super mobileLoginModule:loginModule willHandlePreMobileLogin:preLoginInfo]; } /** Called when mobile login module finishs login. @param loginModule the mobile login module that sends the message @param _postLoginInfo The information mobile login module returns to MSIMobileLoginModuleDelegate */ - (void) mobileLoginModule:(MSIMobileLoginModule *)loginModule willHandlePostMobileLogin:(NSDictionary *)postLoginInfo{ [super mobileLoginModule:loginModule willHandlePostMobileLogin:postLoginInfo]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 300 * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSLog(@"Logging out"); [((MSIAuthenticationModule *)loginModule) logoutFromProject:[MSIProjectInfo defaultProjectInfo]]; [((MSIAuthenticationModule *)loginModule) logoutFromAllProjects]; }); } @end
-
Register the custom login view controller class in the MSIMobileLoginManager so that it is used for the prompt type for which it was designed. You can do this in the application:didFinishLaunchingWithOptions method of the application delegate, using the setView:controller:module:forPromptType API defined in the MSIMobileLoginManager class. The following code snippet shows how to register a custom view controller to be used with the authentication prompt.
// // CustomAppDelegate.m // MicroStrategyMobile // // Copyright (c) 2014 MicroStrategy Inc. All rights reserved. // #import "CustomAppDelegate.h" #import "CustomLoginPromptViewController.h" #import <MicroStrategyMobileSDK/MSIAuthenticationModule.h> #import <MicroStrategyMobileSDK/MSICustomizableAuthenticationPromptView.h> #import <MicroStrategyMobileSDK/MSIMobileLoginManager.h> #import <MicroStrategyMobileSDK/MSIProjectInfo.h> @implementation CustomAppDelegate -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ MSICustomizableAuthenticationPromptView *loginView = [[MSICustomizableAuthenticationPromptView alloc]init]; CustomLoginPromptViewController *loginViewController = [[CustomLoginPromptViewController alloc] init]; MSIAuthenticationModule *loginModule = [[MSIAuthenticationModule alloc] init]; [[MSIMobileLoginManager sharedMobileLoginManager] setView:loginView controller:loginViewController module:loginModule forPromptType:AuthenticationPromptType]; BOOL res = [super application:application didFinishLaunchingWithOptions:launchOptions]; return res; } @end