Viewing the SUP101Appdelegate Files
The SUP101Appdelegate.h and
SUP101Appdelegate.m files are created when you create the
Xcode project; however, you deleted the automatically generated versions and replaced them with
the ones added from the source code ZIP file.
The SUP101Appdelegate files make use of the SUPApplication and
SUPDataVault APIs to show how to store and retrieve sensitive
data (such as Sybase Unwired Platform credentials) using a PIN.
The applicationDidFinishLaunching method checks to see if the application has been run before, then prompts the device user for a PIN to unlock the application; the Sybase Unwired Platform user's password is also requested.
Control passes to the initializeSUP101 method. This code sample does one of two things, depending on whether the application has been run before:
If you are connecting to the Unwired Server through a Relay Server, then you must provide additional information for the database synchronization profile:
The applicationDidFinishLaunching method checks to see if the application has been run before, then prompts the device user for a PIN to unlock the application; the Sybase Unwired Platform user's password is also requested.
Control passes to the initializeSUP101 method. This code sample does one of two things, depending on whether the application has been run before:
- If the application is running for the first time, the sample creates a new SUPDataVault secured with the user-provided PIN, to store the password and other items.
- If the application has been run before, the sample attempts to unlock the existing vault with the provided PIN. If this fails, the application displays an error dialog and exits.
if(self.firstrun) { NSLog(@"Running the app for the first time."); // If the application is being run for the first time, we do the following: // 1. Remove the messaging data vault created by earlier versions of the application, if it exists. // 2. Remove the SUP101 data vault created by earlier versions of the application, if it exists. // 3. Create the messaging vault using the PIN as the password, leaving it unlocked for use by the messaging layer. // 4. Create the SUP101 data vault using the PIN as the password, and store the SUP username/password credentials // and a database encryption key in the vault. // @try { NSLog(@"Delete preexisting messaging vault"); [SUPDataVault deleteVault:kMessagingDataVaultID]; } @catch(NSException *e) { // Ignore any exception } @try { NSLog(@"Delete preexisting SUP101 data vault"); [SUPDataVault deleteVault:kSUP101DataVaultID]; } @catch(NSException *e) { // Ignore any exception } @try { NSLog(@"Create new SUP101 data vault and store credentials and a generated encryption key"); sup101vault = [SUPDataVault createVault:kSUP101DataVaultID withPassword:self.pin withSalt:kSUP101DataVaultSalt]; // creates the vault [sup101vault setString:@"password" withValue:self.SUPPassword]; [sup101vault lock]; } @catch (NSException *exception) { NSLog(@"Exception in creating new SUP101 data vault: %@: %@",[exception name], [exception reason]); } @try { NSLog(@"Create new messaging vault and leave it unlocked"); messagingvault = [SUPDataVault createVault:kMessagingDataVaultID withPassword:self.pin withSalt:kDVStandardSalt]; } @catch (NSException *exception) { NSLog(@"Exception in creating new messaging data vault: %@: %@",[exception name], [exception reason]); } } else { // If the application has been run before, we get the PIN from the user, and use it to unlock the existing messaging data vault // (otherwise the messaging layer cannot start). // // NSLog(@"App has been run before."); @try { NSLog(@"Unlock messaging vault"); messagingvault = [SUPDataVault getVault:kMessagingDataVaultID]; [messagingvault unlock:self.pin withSalt:kDVStandardSalt]; } @catch (NSException *exception) { NSLog(@"Exception unlocking messaging data vault: %@: %@",[exception name],[exception reason]); [self showNoTransportAlert:kSUP101ErrorBadPin]; } }This code sample sets up the Application API settings for connection to the Unwired Server and registers with the Unwired Server.
// Start up the messaging client. This will attempt to connect to the server. If a connection was // established we can proceed with login. See onConnectFailure: for more information about handling connection failure. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onConnectSuccess:) name:ON_CONNECT_SUCCESS object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onConnectFailure:) name:ON_CONNECT_FAILURE object:nil]; self.connectStartTime = [NSDate date]; SUPApplication* app = [SUPApplication getInstance]; @try { sup101vault = [SUPDataVault getVault:kSUP101DataVaultID]; [sup101vault unlock:self.pin withSalt:kSUP101DataVaultSalt]; app.applicationIdentifier = @"sup101"; // Register callback handlers. This should be done before any other SUP code is called. CallbackHandler *cbHandler = [CallbackHandler getInstance]; [SUP101SUP101DB registerCallbackHandler:cbHandler]; ApplicationCallbackHandler *AppCbHandler = [ApplicationCallbackHandler getInstance]; [app setApplicationCallback:AppCbHandler]; SUPConnectionProperties* props = app.connectionProperties; [props setServerName:self.SUPServerName]; [props setPortNumber:[self.SUPServerPort intValue]]; [props setUrlSuffix:@""]; [props setFarmId:self.SUPFarmID]; SUPLoginCredentials* login = [SUPLoginCredentials getInstance]; if(self.SUPManualRegistration) { login.username = self.SUPConnectionName; login.password = nil; props.activationCode = self.SUPActivationCode; } else { login.username = self.SUPUserName; login.password = [sup101vault getString:@"password"]; props.activationCode = nil; } props.loginCredentials = login;This code sample does one of two things depending on whether the application has been run before:
- If the application is running for the first time, the sample creates the SUP101 database, generates an encryption key, and stores it in the data vault.
- If the application has been run before, the sample retrieves the encryption key from the data vault, and sets it in the connection profile so the database can be reused.
// Normally you would not delete the local database. For this simple example, though, // deleting and creating an empty database will cause all data to be sent from the // server, and we can use [CallbackHandler onImportSuccess:] to know when to proceed. [SUP101SUP101DB deleteDatabase]; [SUP101SUP101DB createDatabase]; SUPConnectionProfile *cp = [SUP101SUP101DB getConnectionProfile]; [cp.syncProfile setDomainName:@"default"]; [cp enableTrace:NO]; [cp.syncProfile enableTrace:YES]; // Generate an encryption key for the database. [SUP101SUP101DB generateEncryptionKey]; [SUP101SUP101DB closeConnection]; // Store the encryption key in the data vault for future use. [sup101vault setString:@"encryptionkey" withValue:[cp getEncryptionKey]]; // Since we are creating the database from scratch, we set the encryption key for the new database // If we were using the database from a previous run of the app and not creating it each time, an application should run the code below instead. // To successfully access a previously encrypted database, we set the key used by the connection profile. NSString *key = [sup101vault getString:@"encryptionkey"]; NSLog(@"Got the encryption key: %@",key); [cp setEncryptionKey:key]; [SUP101SUP101DB closeConnection]; [SUP101SUP101DB setApplication:app]; [app registerApplication:0]; } @catch (SUPApplicationTimeoutException* tex) { NSLog(@"%@: %@", [tex name],[tex message]); } @catch (SUPPersistenceException * pe) { NSLog(@"%@: %@", [pe name],[pe message]); [self showNoTransportAlert:kSUP101ErrorFailure]; } @catch (NSException* e) { NSLog(@"%@: %@", [e name],[e reason]); [self showNoTransportAlert:kSUP101ErrorFailure]; } @finally { if([SUPDataVault vaultExists:kSUP101DataVaultID]) [sup101vault lock]; }Once the server connection is made, the onConnectionStatusChanged method in the callback handler posts an ON_CONNECT_SUCCESS notification, and the SUP101AppDelegate's onConnectSuccess method is called. This method performs a database subscription to the Unwired Server, using the user name from the application settings, and the password from the data vault.
If you are connecting to the Unwired Server through a Relay Server, then you must provide additional information for the database synchronization profile:
- Add the certificate file provided by the Relay Server to the Resource folder of your Xcode project.
- Add this
code:
SUPConnectionProfile *sp = [SUP101SUP101DB getSynchronizationProfile]; [sp setNetworkProtocol:@"https"]; // or http [sp setPortNumber:443]; // if http then corresponding port [sp setNetworkStreamParams:@"trusted_certificates=certificateName;compression=zlib;url_suffix=urlsuffixProvidedByTheRelayServer"];
- NetworkProtocol – http or https.
- PortNumber – the correct port number for the selected NetworkProtocol.
- NetworkStreamParams – certificateName: the name of the certificate you added in the
Resource folder.urlsuffixProvidedByTheRelayServer: the URL suffix provided by the Relay Server
Comments
Post a Comment