search cancel

VIP CDK: Apple iOS Authentication Code Snippets

book

Article ID: 150719

calendar_today

Updated On:

Products

VIP Software Development Kit

Issue/Introduction

 

Resolution

Saving a Credential

The following code snippet illustrates how to save the newly created credential into the Symantec Vault for future Security Code retrieval.

NSError *error = [[NSError alloc] init];

if (securityCredential != nil) {

     NSArray *credentialArray = [NSArray arrayWithObjects:securityCredential, nil];

     // store the values in secure storage.

     BOOL isSuccess = [self.vault storeCredential:credentialArray error:&error];

     NSString *credentialId = [securityCredential getCredentialId];

     // Save credentialIds

}

This code performs the following functions:

  1. Puts the credential object into an NSArray.
  2. Saves the array on the mobile device.
  3. Saves the credential ID of the credential.

Note: If you are not using the Vault interface provided by Symantec, you must using the following code snippet to get the credential object as NSData. Save this data in encrypted form on the mobile device (for example, in KeyChain).

 

Credential *securityCredential = nil;

NSData *myData = nil;

NSMutableDictionary *dict = nil;

if (securityCredential != nil) {

     NSString *credentialID = [securityCredential getCredentialId];

     [dict setObject: securityCredential forKey:credentialId];

     myData = [NSKeyedArchiver archivedDataWithRootObject:dict];

     // Store myData (NSData) in secure storage.

}

 

Restoring a Credential

The following code snippet illustrates how to restore a credential from the Symantec Vault in preparation to retrieve a Security Code used for authentication:

// restore values from storage.

 

Credential *securityCredential = nil;

 

NSMutableArray *credentialArray = [[NSMutableArray alloc] init];

NSError *error = [[NSError alloc] init];

// Restore values from storage

BOOL success = [self.vault retrieveCredential:credentialID credentialArray:credentialArray error:&error];

 

 

Note: If you are not using the Vault interface provided by Symantec, you must retrieve Credential Object that was saved in the secure storage using the previous sample code.

 

NSData *credData = nil;

NSMutableDictionary *query;

NSString *credId = nil;

// Get NSData object of credential from the keychain

*dict = [NSKeyedUnarchiver unarchiveObjectWithData:credData];

securityCredential = [dict objectForKey: credId];

 

 

Generating a Security Code

The following code snippet illustrates how to retrieve a Security Code used for authentication:

 

if (success) {

     securityCredential = [credentialArray objectAtIndex:0];

     NSString *code = nil;

     code = [securityCredential getSecurityCode:&error];

}