MicroStrategy ONE

Modify Connection Information with User Input in iOS

Third-party applications can collect connection information from users and use it to connect to Intelligence Server. The values provided by the users override the existing values in the connection property list file (ConnectionInfo.plist).

The code snippets below illustrate how an application uses values entered on a screen to connect to Intelligence Server when a Connect button is tapped. Connection information is first retrieved from ConnectionInfo.plist, and then any information provided by users on the screen is used in place of existing information in ConnectionInfo.plist. The code provided in these snippets allows the server to change multiple times in the same application run.

The code snippet below illustrates how the application initially connects to Intelligence Server when a Connect button is tapped, using only the information in ConnectionInfo.plist. The connection information, such as the server name or project name, are returned to the client side after a successful login to the server.

Copy
- (IBAction) handleConnectButton {
   NSString* shortName = @"ConnectionInfo.plist";
   [ConnectionHelper resetConfig: shortName];
   NSString* pathToPList = [[NSBundle mainBundle] pathForResource: shortName ofType: @""];
 
   NSDictionary* dictionary =  [NSDictionary dictionaryWithContentsOfFile: pathToPList];
   NSString* serverName =  [dictionary objectForKey: @"webServer"];
   NSString* projectName = [dictionary objectForKey: @"project"];
 
   MSIProjectInfo* projectInfo = [URLLaunchHelper newProjectInfoWithServerName: serverName projectName: projectName];
   [projectInfo retain];
   @try {
      // getSessionState: will throw an exception if login was not successful.
      NSString* sessionState = [[MSIServiceFactory getServiceFactory] getSessionState: projectInfo];
      [mServerResultTextView setText: @"Login Succeeded!"];
      [mServerResultTextView setTextColor: [UIColor greenColor]];
   }
   @catch (NSException* ex) {
      [mServerResultTextView setText: @"Login failed!"];
      [mServerResultTextView setTextColor: [UIColor redColor]];
   }
   @catch (id unhandledException) {
      // code that handles this exception
      [mServerResultTextView setText: @"Login failed with unexpected errors!"];
      [mServerResultTextView setTextColor: [UIColor redColor]];
   }
   @finally {
      [projectInfo release];
   }
}

The code snippets below illustrate how an application connects to Intelligence Server when a Connect button is tapped, using both the information in ConnectionInfo.plist and information entered by a user on a screen. In the code snippet, a user can enter a new user name, user password, and project name, but any connection information can be overridden.

First, you declare the text fields on the screen where users can enter new connection information, the Connect button, and the text area where the results of the login attempt are displayed—"Login succeeded" or "Login failed". You also declare the navigation controller that lets you add a toobar to the application and themethod that will reset the configuration information inConnectionInfo.plist.

Copy
IBOutlet UIButton* mConnectButton;
IBOutlet UITextField* mUserNameField;
IBOutlet UITextField* mPasswordField;
IBOutlet UITextField* mProjectNameField;
IBOutlet UITextView* mServerResultTextView;
 
UINavigationController* mNavController;
 
+(void) resetConfig:(NSString*) plistFile;

You provide code for the resetConfig method that clears the preferences store and then resets it with the configuration information in ConnectionInfo.plist. This method ensures that the latest values are used, allowing the server to change multiple times in the same application run.

Copy
+ (void) resetConfig: (NSString*) plistFile {
   // clear configurations in memory
   MSIDevicePreferences* dp = [[[MSIDevicePreferences alloc] init] autorelease];
   [[MSIPreferencesStore preferencesStore] updatePreferences: dp];
   [[MSIServiceFactory getServiceFactory] clearSessions];
   [MSIGeneric clearCertificates];
   // restore configurations from plist file
   [MSIGeneric setupConnectInfoFromPlist: plistFile];
}

Provide the code for the viewDidLoad method to connecting the text fields where the user enters new connection information.

Copy
- (void)viewDidLoad {
   [super viewDidLoad];
   mUserNameField.delegate = self;
   mPasswordField.delegate = self;
   mProjectNameField.delegate = self;
}

Then, you provide code to get the current information in ConnectionInfo.plist when the Connect button is tapped. You invoke the resetConfig method and pass it the connection information. Because the preferences store is read-only, you make a copy of the preferences and modify that copy.

Copy
- (IBAction) handleConnectButton {
   NSString* shortName = @"ConnectionInfo.plist";
   [ConnectionHelper resetConfig:shortName];
   MSIPreferencesStore* prefsStore = [MSIPreferencesStore preferencesStore ];
 
   MSIDevicePreferences* dp = [prefsStore preferencesCopy];
   WebServerList* wsl = [dp getWebServers];
   if ( [wsl size] < 1 )
      return;
   WebServerConfiguration* wsc = nil;
   wsc = [wsl getWebServer: [wsl size] - 1];
   if (wsc == nil)
      return;
 
   MSIProjectList* prList = [wsc projects];
   if ( [prList size] < 1 )
      return;
 

Next, you provide code to get the new connection information entered by the user on the screen and store it in the user preferences.

Copy
   MSIProjectConfiguration* project = nil;
   project = [prList getProject: [prList size] - 1];
   NSString* newProjectName = [mProjectNameField text];
   project.projectName = newProjectName;
 
   NSString* newUserName = [mUserNameField text];
   project.credentials.login = newUserName;
   NSString* newPassword = [mPasswordField text];
   project.credentials.password = newPassword;
   // Store the results now in order to make them take effect
   [prefsStore storePreferences: dp];
   [dp release];

Finally, you provide code that uses the modified connection settings to try to connect to Intelligence Server. You also provide code to set the text that will be displayed on the screen to indicate whether login succeeded or failed.

Copy
   NSString* pathToPList = [[NSBundle mainBundle] pathForResource: shortName ofType: @""];
   NSDictionary* dictionary =  [NSDictionary dictionaryWithContentsOfFile: pathToPList];[
   NSString* serverName =  [dictionary objectForKey: @"webServer"];
 
   MSIProjectInfo* projectInfo = [URLLaunchHelper newProjectInfoWithServerName: serverName projectName: newProjectName];
   [projectInfo retain];
   @try {
      // getSessionState: will throw an exception if login was not successful.
      NSString* sessionState = [[MSIServiceFactory getServiceFactory] getSessionState: projectInfo];
      [mServerResultTextView setText: @"Login succeeded!"];
      [mServerResultTextView setTextColor: [UIColor greenColor]];
   }
   @catch ( NSException* ex ) {
      [mServerResultTextView setText: @"Login failed!"];
      [mServerResultTextView setTextColor: [UIColor redColor]];
   }
   @catch ( id unhandledException ) {
      // code that handles this exception
      [mServerResultTextView setText: @"Login failed with unexpected errors!"];
      [mServerResultTextView setTextColor: [UIColor redColor]];
   }
   @finally {
      [projectInfo release];
   }
}