首页 文章

使用saveToURL保存到iCloud:forSaveOperation:completionHandler:失败

提问于
浏览
0

我试图将对象字典保存到iCloud,但是当我这样做时,方法saveToURL:forSaveOperation:completionHandler:failed . 我也试图覆盖:

- (BOOL)writeContents:(id)contents
    andAttributes:(NSDictionary *)additionalFileAttributes
      safelyToURL:(NSURL *)url
 forSaveOperation:(UIDocumentSaveOperation)saveOperation
            error:(NSError **)outError

当然,超级电话也会返回false . 然而,我本来希望阅读错误,但是当我尝试使用localizedError时,编译器报告错误声称它不是结构或联合 . 这是完整的代码:

-(instancetype)initWithSingleton{
NSURL *ubiq = [[NSFileManager defaultManager]
               URLForUbiquityContainerIdentifier:nil];
NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:
                             @"Stops"] URLByAppendingPathComponent:kFILENAME];
NSLog(@"file url=%@", ubiquitousPackage);
self=[self initWithFileURL:ubiquitousPackage];
if (self!=nil){
    self.favoriteStops=[[NSMutableDictionary alloc] init];
    NSURL *ubiq = [[NSFileManager defaultManager]
                   URLForUbiquityContainerIdentifier:nil];
    if (ubiq) {
        NSLog(@"iCloud access at %@", ubiq);
        [self loadDocument];
    } else {
        NSLog(@"No iCloud access");
    }
}
return self;
}
#define kFILENAME @"favorite.dox"

- (void)loadData:(NSMetadataQuery *)query {

if ([query resultCount] == 1) {
    NSMetadataItem *item = [query resultAtIndex:0];
    NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLResponse *response;
    NSData *GETReply= [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    NSMutableDictionary* dict=[NSKeyedUnarchiver unarchiveObjectWithData:GETReply];
    [self setFavoriteStops:dict];
    NSLog(@"favorites: %@", favoriteStops);
    [self openWithCompletionHandler:^(BOOL success) {
        if (success) {
            NSLog(@"iCloud document opened");
        } else {
            NSLog(@"failed opening document from iCloud");
        }
    }];
}
}

- (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];

}
- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName
               error:(NSError **)outError
{
if ([contents length] > 0) {
    [self setFavoriteStops:[NSKeyedUnarchiver unarchiveObjectWithData:contents]];
}
return YES;
}

- (BOOL)writeContents:(id)contents
    andAttributes:(NSDictionary *)additionalFileAttributes
      safelyToURL:(NSURL *)url
 forSaveOperation:(UIDocumentSaveOperation)saveOperation
            error:(NSError **)outError{
//logging
NSString *str;
str= [[NSString alloc] initWithData:contents encoding:NSUTF8StringEncoding];
NSLog(@"saving data %@", str);
//logging
NSMutableDictionary *dict=[NSKeyedUnarchiver unarchiveObjectWithData:contents];
NSLog(@"dict=%@", dict);

BOOL success= [super writeContents:contents
        andAttributes:additionalFileAttributes
          safelyToURL:url
     forSaveOperation:saveOperation
                error:outError];
NSLog(@"error :%@", outError.localizedDescription) //syntax error
return success;
}

-(void) save{
NSLog(@"file url=%@", [self fileURL]);
[self saveToURL:[self fileURL]
forSaveOperation:UIDocumentSaveForOverwriting
completionHandler:^(BOOL success) {
  if (success) { //this returns false
      [self openWithCompletionHandler:^(BOOL success) {
          NSLog(@"new document saved on iCloud");
      }];
  } else {
      NSLog(@"error in iCloud Saving");
  }
 }];
}

- (id)contentsForType:(NSString *)typeName error:(NSError **)outError
{
NSLog(@"favorite stops=%@ class=%@", self.favoriteStops, [favoriteStops class]);
NSData *archivedData=[NSKeyedArchiver archivedDataWithRootObject:self.favoriteStops];
return archivedData;

}

当我记录要保存的URL时,即:

file:///private/var/mobile/Library/Mobile%20Documents/iCloud〜com~information~inArrivo/Stops/favorite.dox

当我检查调试器上的错误时,它是:

Error Domain = NSCocoaErrorDomain Code = 4“操作无法完成 . (Cocoa错误4)”UserInfo = 0x17bd6cb0 {NSFileNewItemLocationKey = file:/// private / var / mobile / Applications / 445778BF-86AF-4DE3-9E1B -BAC8F79D14D0 / tmp /(A%20Document%20Being%20Saved%20By%20In%20Arrivo%20HD)/favorite.dox,NSFileOriginalItemLocationKey = file:/// private / var / mobile / Library / Mobile%20Documents / iCloud~com~信息~inArrivo / Stops / favorite.dox,NSUnderlyingError = 0x17bfa860“操作无法完成 . (Cocoa error 4.)”,NSURL = file:/// private / var / mobile / Library / Mobile%20Documents / iCloud 〜COM〜〜资料inArrivo /停止/ favorite.dox}

我该如何解决或者至少知道更多?

1 回答

  • 0

    TSI Apple团队回答了我并为我提供了一个 Worker 阶级 . 在检查他们的版本时,我的更改似乎归结为更新 - (void)queryDidFinishGathering:(NSNotification *)通知;通过增加:

    if (query.results == 0)
        {
            [self save];  // no favorites file, so create one
        }
    

    如下:

    - (void)queryDidFinishGathering:(NSNotification *)notification {
    
        NSMetadataQuery *query = [notification object];
        [query disableUpdates];
        [query stopQuery];
    
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:NSMetadataQueryDidFinishGatheringNotification
                                                  object:query];
    //•• added
    if (query.results == 0)
        {
            [self save];  // no favorites file, so create one
        }
    
       [self loadData:query];
    
       _query = nil;
    }
    

相关问题