- 26 Dec 2024
- 3 Minutes à lire
- SombreLumière
- PDF
iOS integration
- Mis à jour le 26 Dec 2024
- 3 Minutes à lire
- SombreLumière
- PDF
To use this feature
Import Shield.h into your application.
Initialize the SecureLocalStorage instance in your application.
Function retrieving data from the Secure App ROM.
#import <Shield.h> ... - (void)secureLocalStorageInit { SecureLocalStorage *storage = [SecureLocalStorage defaultStorage]; ... } ...
(BOOL)setValue:(NSData*)value forKey:(NSString*)key error:(NSError**)error
Stores data in Secure Local Storage.
The data in the value argument will be associated with the name key in Secure Local Storage. The key can later be used to refer to the data, e.g. to retrieve it or delete it.
If there is already data associated with key in Secure Local Storage, the old data will be replaced.
The key argument is required to be non-nil. If key is nil, this method will fail, and may potentially cause the app to crash. The value argument can be nil, which will associate the nil object with key'
This method returns YES on success and NO on failure. On failure, the error argument will be set to a description of the detected failure. The most likely failure that can happen is that writing to disk fails, probably because the disk is full. If there is no need to handle specific errors, error can be nil.
ViewController.m (Objective-C)
#import <Shield.h> ... - (void)saveData { ... NSData *dataToStore = [@"Sensitive data to be stored" dataUsingEncoding:NSUTF8StringEncoding]; NSString *key = @"Data tag"; NSError *error = nil; if (![storage setValue:dataToStore forKey:key error:&error]) { // Handle error, if desired. } } ...
(NSData*)valueForKey:(NSString*)key error:(NSError**)error
Retrieve data from Secure Local Storage.
The data associated with key in Secure Local Storage is returned. If no data is associated with key, nil is returned. In this case error is set to the error code ItemNotFound in the error domain ShieldSecureLocalStorageErrorDomain.
The key argument is required to be non-nil. If key is nil, this method will fail, and may potentially cause the app to crash.
This method returns the requested data on success and nil on failure. On failure, error is set to a description of the detected failure. Besides ItemNotFound, the most likely failure that can happen is that reading from disk fails due to hardware errors. If there is no need to handle specific errors, error can be nil.
If the data on disk is corrupt, most likely this method will return the ItemNotFound error, as if there is no data associated with key.
ViewController.m (Objective-C)
- (void)retrieveData { ... NSString *key = @"Data tag"; NSError* error = nil; NSData *dataRetrieved = [storage valueForKey:key error:&error]; if (dataRetrieved == nil) { if ([error.domain isEqualToString:ShieldLocalStorageErrorDomain] && error.code == ItemNotFound) { // Handle the case where the requested data was not found in // Secure Local Storage. } else { // Handle error during data retrieval. } } ... }
(BOOL)removeObjectForKey:(NSString*)key error:(NSError**)error
Delete data from Secure Local Storage.
Any data associated with key in Secure Local Storage is deleted.
The key argument is required to be non-nil. If key is nil, this method will fail, and may potentially cause the app to crash.
This method returns YES on success and NO on failure. On failure, the error argument is set to a description of the detected failure. If there is no need to handle specific errors, error can be nil.
ViewController.m (Objective-C)
- (void) removeData { ... NSString *key = @"Data tag"; [storage removeObjectForKey:key error:nil]; } ...
Swift example of implementation
Secure Local Storage Example (Swift)
import ShieldSDK ... class SLSExample { let storage: SecureLocalStorage // Function to save data func saveData() { let dataToStore: Data = "Sensitive data to be stored.".data(using: .utf8)! let key: String = "Data tag" storage.setValue(dataToStore, forKey: key) } func retrieveData() { let key: String = "Data tag" let dataRetrieved: Data? = storage.value(forKey: key) guard let data = dataRetrieved else { // error handling return } // SLS had a value for the key, access it through data } func removeData() { let key: String = "Data tag" storage.removeObject(forKey: key) } }