2014-01-08 11 views
1

如何同步的/到MAC OSX與如何同步從iPad/iPhone的一個NSDocument到Mac OSX與iCloud的

我設法得到它的工作!在Mac OSX到iPhone/iPad上而不是從的iPad/iPhone到Mac OSX

這裏是我的代碼從子類文件上

頭文件

#import <Cocoa/Cocoa.h> 
#import <Foundation/Foundation.h> 

@interface subclassedNSDocument : NSDocument 

@property (strong) NSData *myData; 

@end 

實現文件:

- (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 AppDelegate.m file:

#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 
} 

的問題是: ,我還沒有得到工作的唯一的事情是,如果我更改iPad上的文檔的數據,在Mac應用程序不要求從更新readFromData方法iCloud,有誰知道我錯過了什麼?

在iOS上,每當更改iCloud中的UIDocument時,都會自動調用等效方法loadFromContents。在OS X上,readFromData在加載時被調用一次,但從未再次調用。

希望我的代碼可以幫助,對我而言,它是從Mac到iPad的單向工作方式。

+0

嗨,你有沒有解決它?我面臨同樣的問題。我可以從調試器看到iCloud正在下載一些數據,但觀察者從未觸發。我已經嘗試添加'NSMetadataQueryDidUpdateNotification'觀察者,仍然沒有去。 – soemarko

回答

1

我自己修復了。

顯然這只是一個執行不力和過於複雜的示例代碼。

applicationDidFinishLaunching:我們只需要

_query = [[NSMetadataQuery alloc] init]; 
[_query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]]; 
NSPredicate *pred = [NSPredicate predicateWithFormat: @"%K == %@", NSMetadataItemFSNameKey, kFILENAME]; 
[_query setPredicate:pred]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryUpdated:) name:NSMetadataQueryDidUpdateNotification object:_query]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryUpdated:) name:NSMetadataQueryDidFinishGatheringNotification object:_query]; 
[_query startQuery]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataReloaded:) name:@"dataModified" object:nil]; 

,並添加以下方法:

- (void)queryUpdated:(NSNotification *)notification { 
    NSLog(@"queryUpdated called..."); 
    NSMetadataQuery *query = [notification object]; 
    [query disableUpdates]; 

    if ([query resultCount] == 1) { 
     // update views, etc. here 
    } 

    [query enableUpdates]; 
} 

然後刪除原始樣品的所有不必要的方法。