I want to transfer my NSObject "RND_PatientData" which is encrypted from my application to another person using the application using AirDrop. At present my data is saved using this code:
- (RND_PatientData *)data {
if (_data != nil) return _data;
NSString *dataPath = [_docPath stringByAppendingPathComponent:kDataFile];
NSData *codedData = [[NSData alloc] initWithContentsOfFile:dataPath];
if (codedData == nil) return nil;
NSString *deviceName = [[UIDevice currentDevice] name];
NSData *decryptedData = [RNDecryptor decryptData:codedData withSettings:kRNCryptorAES256Settings password:deviceName error:nil];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:decryptedData];
_data = [unarchiver decodeObjectForKey:kDataKey];
[unarchiver finishDecoding];
return _data;
}
- (void)saveData {
if (_data == nil) return;
[self createDataPath];
NSString *dataPath = [_docPath stringByAppendingPathComponent:kDataFile];
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:_data forKey:kDataKey];
[archiver finishEncoding];
NSError *error;
NSString *deviceName = [[UIDevice currentDevice] name];
NSData *encryptedData = [RNEncryptor encryptData:data
withSettings:kRNCryptorAES256Settings
password:deviceName
error:&error];
[encryptedData writeToFile:dataPath atomically:YES];
This data is also encrypted using the device UDID and the RNDecryptor library before being saved. Now, I would like the perosn to be able to transfer the data via AirDrop. Is it better to decrypt the data on the sender phone and then transfer it unencrypted and encrypt it on the receiver phone using the same framework or to transfer it encrypted and decrypt it using the sender device UDID?
I load my current data as a NSMutableArray :
_patients = [RND_PatientDB loadDocs];
The method goes as follow:
+ (NSMutableArray *)loadDocs {
// Get private docs dir
NSString *documentsDirectory = [RND_PatientDB getPrivateDocsDir];
NSLog(@"Loading patients from %@", documentsDirectory);
// Get contents of documents directory
NSError *error;
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
if (files == nil) {
NSLog(@"Error reading contents of documents directory: %@", [error localizedDescription]);
return nil;
}
// Create Patients for each file
NSMutableArray *retval = [NSMutableArray arrayWithCapacity:files.count];
for (NSString *file in files) {
if ([file.pathExtension compare:@"patients" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:file];
RND_Patient *doc = [[RND_Patient alloc] initWithDocPath:fullPath];
[retval addObject:doc];
}
}
return retval;
}
+ (NSString *)nextPatientDocPath {
// Get private docs dir
NSString *documentsDirectory = [RND_PatientDB getPrivateDocsDir];
// Get contents of documents directory
NSError *error;
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
if (files == nil) {
NSLog(@"Error reading contents of documents directory: %@", [error localizedDescription]);
return nil;
}
// Search for an available name
int maxNumber = 0;
for (NSString *file in files) {
if ([file.pathExtension compare:@"patients" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
NSString *fileName = [file stringByDeletingPathExtension];
maxNumber = MAX(maxNumber, fileName.intValue);
}
}
// Get available name
NSString *availableName = [NSString stringWithFormat:@"%d.patients", maxNumber+1];
return [documentsDirectory stringByAppendingPathComponent:availableName];
}
I've tried following instructions here to send NSArray via AirDrop, but I cannot understand how the certificate works. Anyone has ressources I can use to learn more about this procedure? Also am I doing it the right way or is there an easier method to send my NSMutableArray of _patients comprised of RND_PATIENT NSObject to another person using my application?