These instructions are only applicable to users who do not have a persistent API key. Persistent API keys do not expire and can be included in the Info.plist
file.
1. Obtaining an API Key
When using the SDK for the first time, you need to obtain an API key. To do this, follow the steps below:
Create an instance of
NMApiCredentials
with mandatory parameters (account ID, password, email ID, service ID) with one of the methods provided.Call the
- (void)obtainApiKeyWithCredentials:completion:
method, to request a new API Key. A result is expected to come in the completion block.
Example:
NMApiCredentials *credentials = [NMApiCredentials alloc] initWithAccountId:@"yourAccountId" emailId:@"yourEmail" password:@"yourPAssword" serviceId:@(1234); [navmiiSDK obtainApiKeyWithCredentials:credentials completion:^(NSString * _Nullable apiKey) { // Store the received API key securely // Next time pass the key to the SDK via // [navmiiSDK setApiKey:apiKey]; }]; });
2. Storing the API Key Securely
After obtaining the API key, store it securely within your app. Keychain will do the best. This ensures the key is available for use in future SDK initialization without needing to request a new key every time.
3. Setting the API Key
When initializing the SDK, pass the stored API key to the NMConfigurationSettings
. To update the key after the SDK has been initialized, use the - (void)setApiKey:
method:
NSString *storedApiKey = // Retrieve the stored API key [navmiiSDK setApiKey:storedApiKey];
4. Handling API Key Expiration
The API key can expire, causing the SDK methods requiring authentication to return an ApiKeyExpired
status. There are two ways to handle this situation:
For SDK methods explicitly called by users that require authentication, handle the
ApiKeyExpired
status by obtaining a new API key as described in step 1.For methods that implicitly make online requests, set up a listener to be notified of API key expiration. Follow the steps below:
Create a class that extends
NMApiKeyExpiredListener
.Implement the
- (void)onApiKeyExpired
method to handle the expiration event.Add the listener using the
- (void)addApiKeyExpiredListener
method.When the listener is triggered, obtain a new API key as described in step 1.
Example:
@interface MyApiKeyExpiredListener: NSObject<NMApiKeyExpireListener> @end @implementation MyApiKeyExpiredListener - (void)onApiKeyExpired { } @end MyApiKeyExpiredListener *listener = [[MyApiKeyExpiredListener alloc] init]; [navmiiSDK addApiKeyExpiredListener: listener];
The listener object retains when passed to the method. It you need to remove it when it's no longer needed using the
- (void)removeApiKeyExpiredListener:
method:
[navmiiSDK addApiKeyExpiredListener: listener];
5. Checking the API Key expiration status
The API Key expiration status can be checked with - (BOOL)isApiKeyExpired
method.
BOOL isApiKeyExpired = [navmiiSDK isApiKeyExpired];