How to sync nsdocument from ipad / iphone on mac osx with icloud
I managed to get it to work! from Mac osx to iPhone / iPad, but not from iPad / iPhone to Mac OSX
Here is my code from a subclass of the nsdocument file on osx :
Header file:
Implementation File:
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError { BOOL readSuccess = NO; if (data) { readSuccess = YES; [self setMyData:data]; } [[NSNotificationCenter defaultCenter] postNotificationName:@"dataModified" object:self]; return readSuccess; } - (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError { if (!myData && outError) { *outError = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileWriteUnknownError userInfo:nil]; } return myData; }
and in the file AppDelegate.m:
#define kFILENAME @"mydocument.dox" - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]; if (ubiq) { NSLog(@"iCloud access at %@", ubiq); // TODO: Load document... [self loadDocument]; } else { NSLog(@"No iCloud access"); } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataReloaded:) name:@"dataModified" object:nil]; } - (void)update_iCloud { NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]; NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:kFILENAME]; self.doc.myData = [NSKeyedArchiver archivedDataWithRootObject:[@"Your Data Array or any data", nil]]; [self.doc saveToURL:ubiquitousPackage ofType:@"dox" forSaveOperation:NSSaveOperation error:nil]; } - (void)loadData:(NSMetadataQuery *)query { if ([query resultCount] == 1) { NSMetadataItem *item = [query resultAtIndex:0]; NSURL *url = [item valueForAttribute:NSMetadataItemURLKey]; NSLog(@"url = %@",url); subclassedNSDocument *doc = [[subclassedNSDocument alloc] initWithContentsOfURL:url ofType:@"dox" error:nil]; [doc setFileURL:url]; self.doc = doc; } else { NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]; NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:kFILENAME]; dataUrls *doc = [[dataUrls alloc] init]; [self.doc setFileURL:ubiquitousPackage]; self.doc = doc; [self.doc saveToURL:ubiquitousPackage ofType:@"dox" forSaveOperation:NSSaveOperation error:nil]; } } - (void)queryDidFinishGathering:(NSNotification *)notification { NSMetadataQuery *query = [notification object]; [query disableUpdates]; [query stopQuery]; [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:query]; _query = nil; [self loadData:query]; } - (void)loadDocument { NSMetadataQuery *query = [[NSMetadataQuery alloc] init]; _query = query; [query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]]; NSPredicate *pred = [NSPredicate predicateWithFormat: @"%K == %@", NSMetadataItemFSNameKey, kFILENAME]; [query setPredicate:pred]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:query]; [query startQuery]; } - (void)dataReloaded:(NSNotification *)notification { self.doc = notification.object; NSArray *arrFromCloud = [NSKeyedUnarchiver unarchiveObjectWithData:self.doc.myData]; //Update you UI with new data }
Question: The only thing that did not work out for me is that if I change the document data on the iPad, the Mac application will not call the readFromData method to update from iCloud, does anyone know what I donβt see?
On iOS, the equivalent loadFromContents method is called automatically every time a UIDocument in iCloud. In OS X, readFromData is called once at boot time, but never called again.
I hope my code can help, for me it works in one direction from Mac to iPad.
source share