...
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:apiKeyType:completion:
method, to request a new API Key. A result is expected to come in the completion block. A requested API Key Type should be specified.Check received API Key and
NSError
object. Non-empty error instance indicates a failed request. A type of received error. It’s error code corresponds an error type, specified inNMApiKeyObtainError
.
Example:
Objective-C
Code Block | ||
---|---|---|
| ||
NMApiCredentials *credentials = [NMApiCredentials alloc] initWithAccountId:@"yourAccountId"
emailId:@"yourEmail"
password:@"yourPAssword"
serviceId:@(1234);
[NMSdk obtainApiKeyWithCredentials:credentials apiKeyType:NMApiKeyTypeDefault completion:^(NSString * _Nullable apiKey, NSError * _Nullable error) {
// if (error) {
// Handle error
// }
// if (apiKey && [apiKey lenght] > 0) {
// Store the received API key securely
// Now you can start the SDK
// [navmiiSDK startWithSettings:configurationSettings completion:nil];
// }
// Next time pass the key to the SDK via NMConfigurationSettings object
// NMConfigurationSettings *configurationSettings = [NMConfigurationSettings new];
// configurationSettings.apiKey = apiKey;
// [navmiiSDK startWithSettings:configurationSettings completion:nil];
}];
}); |
Swift
Code Block | ||
---|---|---|
| ||
func setupSdk() async {
do {
let apiKey = try await requestApiKey()
// Store the received API key securely
try await startSDK(with: apiKey)
} catch {
// Handle error
print(error)
}
}
func requestApiKey() async throws -> String {
let credentials = NMApiCredentials(accountId: "12345678",
emailId: "your@email.com",
password: "Password",
serviceId: 123)
return try await NMSdk.obtainApiKey(withCredentials: credentials, apiKeyType: .default)
}
func startSDK(with apiKey: String) async throws {
let settings = NMConfigurationSettings()
settings.apiKey = apiKey
return try await sdk.start(withSettings: settings)
} |
2. Storing the API Key Securely
...
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)updateApiKey:withType:
method:
Objective-C
Code Block | ||
---|---|---|
| ||
NSString *storedApiKey = // Retrieve the stored API key
[navmiiSDK updateApiKey:storedApiKey withType:NMApiKeyTypeDefault]; |
Swift
Code Block | ||
---|---|---|
| ||
let storedApiKey = // Retrieve the stored API key
sdk.updateApiKey(apiKey, withType: .default) |
4. Handling API Key Expiration
...
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:
Objective-C
Code Block | ||
---|---|---|
| ||
@interface MyApiKeyExpiredListener: NSObject<NMApiKeyExpireListener>
@end
@implementation MyApiKeyExpiredListener
- (void)onApiKeyExpired:(NMApiKeyType)apiKeyType {
}
@end
MyApiKeyExpiredListener *listener = [[MyApiKeyExpiredListener alloc] init];
[navmiiSDK addApiKeyExpiredListener: listener]; |
Swift
Code Block | ||
---|---|---|
| ||
class MyApiKeyExpiredListener: NSObject, NMApiKeyExpireListener { func onApiKeyExpired(_ apiKeyType: NMApiKeyType) { } } let listener = MyApiKeyExpiredListener() sdk.addListener(listener: 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:
Objective-C
Code Block | ||
---|---|---|
| ||
[navmiiSDK addApiKeyExpiredListenerremoveApiKeyExpiredListener: listener]; |
Swift
Code Block | ||
---|---|---|
| ||
sdk.removeListener(listener: listener) |
5. Checking the API Key expiration status
The API Key expiration status can be checked with - (BOOL)isApiKeyExpiredisApiKeyExpiredWithType:
method.
Objective-C
Code Block | ||
---|---|---|
| ||
BOOL isApiKeyExpired = [navmiiSDK isApiKeyExpiredWithType: isApiKeyExpiredNMApiKeyTypeDefault]; |
Swift
Code Block | ||
---|---|---|
| ||
let isApiKeyExpired = sdk.isApiKeyExpired(withType: .default) |
6. Error handling
+ (void)obtainApiKeyWithCredentials:completion:
method provides a convenient way to handle errors received on obtaining process. In the callback NSError
shares an error code. Possible values are:
NMApiKeyObtainErrorNoError - means no error received, API Key delivered successfully.
NMApiKeyObtainErrorUnknown - unknown error received.
NMApiKeyObtainErrorNetwork - networking error.
NMApiKeyObtainErrorBadResponse - no API Key obtained, check credentials submitted.
NMApiKeyObtainErrorBadKeyType - unsupported API Key Type provided.