2012-02-18 49 views
0

我是iPhone开发新手。从iCloud获取文档给我3/4内容的文档不是完整的内容

我在我的应用程序中集成了iCloud存储。我成功地在iCloud上传文件。

我的文档大小约为126799字节。在iCloud上传期间,我确保通过在控制台上打印其长度和内容,将正确的文档上传到iCloud。但是,当我从iCloud获取文档时,它只能提供该文档内容的3/4。我也通过打印它的长度和内容在控制台上进行了检查。

/////====== variables are declared in interface file 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  


    NSURL *ubiq = [[NSFileManager defaultManager] 
       URLForUbiquityContainerIdentifier:nil]; 
    if (ubiq) 
    { 
     NSLog(@"iCloud access at %@", ubiq); 
     // TODO: Load document... 
     [self loadDocument]; 
    } 
    else 
    { 
     NSLog(@"No iCloud access"); 
    } 
} 



- (void)loadDocument{ 

    NSMetadataQuery *query = [[NSMetadataQuery alloc] init]; 
    _query = query; 
    [query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]]; 


    NSString *filename = @"supplimentlistdescription.txt"; 
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"%K like '%@'",filename,NSMetadataItemFSNameKey]; 
    [query setPredicate:pred]; 
    [[NSNotificationCenter defaultCenter] 
     addObserver:self 
     selector:@selector(queryDidFinishGathering:) 
     name:NSMetadataQueryDidFinishGatheringNotification 
     object:query]; 

    [query startQuery]; 
} 


- (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)loadData:(NSMetadataQuery *)query { 

    if ([query resultCount] == 1) 
    { 

     NSMetadataItem *item = [query resultAtIndex:0]; 
     NSURL *url = [item valueForAttribute:NSMetadataItemURLKey]; 
     Note *doc = [[Note alloc] initWithFileURL:url]; 
     self.doc = doc; 
     [self.doc openWithCompletionHandler:^(BOOL success) 
      { 
       if (success) 
       {     
        NSLog(@"iCloud document opened");      
       } 
       else 
       {     
        NSLog(@"failed opening document from iCloud");     
       } 
      } 
     ]; 
    } 
    else 
    { 

     NSFileManager *filemgr = [NSFileManager defaultManager]; 
     NSString *fileurlstring = [NSString stringWithFormat:@"Documents/Federal Rules of Civil Procedure"]; 
     NSLog(@"fileurlstring:%@",fileurlstring); 
     ubiquityURL = [[filemgr URLForUbiquityContainerIdentifier:nil]   
      URLByAppendingPathComponent:fileurlstring]; 
     [ubiquityURL retain]; 
     NSLog(@"ubiquityURL1:%@",ubiquityURL); 

     if ([filemgr fileExistsAtPath:[ubiquityURL path]] == NO) 
     { 
      [ubiquityURL retain]; 

      [filemgr createDirectoryAtURL:ubiquityURL withIntermediateDirectories:YES attributes:nil error:nil]; 
      [ubiquityURL retain]; 

     } 

     ubiquityURL = [ubiquityURL URLByAppendingPathComponent:@"supplimentlistdescription.txt"]; 
     [ubiquityURL retain]; 
     NSLog(@"ubiquityURL:%@",ubiquityURL); 

     Note *doc = [[Note alloc] initWithFileURL:ubiquityURL]; 
     self.doc = doc; 

     [doc saveToURL:[doc fileURL] 
      forSaveOperation:UIDocumentSaveForCreating 
      completionHandler:^(BOOL success) 
      {    
       if (success) { 
        [doc openWithCompletionHandler:^(BOOL success) 
         {     
          NSLog(@"new document opened from iCloud");     
         } 
        ];     
       } 
      } 
     ]; 
    } 
} 

-

///Note.h 
#import <UIKit/UIKit.h> 
@interface Note : UIDocument 

@property (strong) NSString * noteContent; 
@end 

-

#import "Note.h" 
@implementation Note 
@synthesize noteContent; 

- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName 
    error:(NSError **)outError 
{ 

    if ([contents length] > 0) 
    { 
     self.noteContent = [[NSString alloc] 
          initWithBytes:[contents bytes] 
          length:[contents length] 
          encoding:NSUTF8StringEncoding]; 
     NSLog(@"loadFromContents1"); 
     NSLog(@"noteContent:%@",noteContent); 
     NSLog(@"noteContent.length:%d",noteContent.length); 
    } 
    else 
    { 
     // When the note is first created, assign some default content 
     self.noteContent = @"Empty"; 
    } 

    return YES; 
} 


- (id)contentsForType:(NSString *)typeName error:(NSError **)outError 
{ 
    if ([self.noteContent length] == 0) 
    { 
     //self.noteContent = @"Empty"; 

     NSString *FolderName = @"Federal Rules of Civil Procedure"; 
     NSString *fileName = @"supplimentlistdescription.txt"; 

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     [FolderName retain]; 
     NSString *fileName1 = [NSString stringWithFormat:@"%@/%@/%@",documentsDirectory,FolderName, fileName]; 
     NSLog(@"fileName1:%@",fileName1); 

     NSData *data = [[NSData alloc]initWithContentsOfFile:fileName1]; 
     noteContent =[[NSString alloc]initWithData:data encoding:NSMacOSRomanStringEncoding]; 
     NSLog(@"noteContent:%@",noteContent); 
     NSLog(@"noteContent.length:%d",noteContent.length); 
    } 

    return [NSData dataWithBytes:[self.noteContent UTF8String] 
         length:[self.noteContent length]]; 
} 

@end 

你能告诉我什么可以是问题?任何建议将不胜感激。谢谢

+0

你可能会展示一些代码来获得一个解决方案。 – 2012-02-18 06:20:54

+0

我已添加代码。请检查它并让我知道代码中的错误。谢谢 – user1217675 2012-02-18 07:16:50

+0

我看到的唯一的“显而易见的”事情就是在最后,你正在使用来自utf8字符串的字节和noteContent的长度来执行[NSData dataWithBytes:length:]。 UTF-8的长度不一定与NSString的长度相同。除此之外,在调试器中发现这个问题可能比阅读代码更容易。 – 2012-02-18 08:23:15

回答

0

我得到了像你以前一样的问题。

您应该使用

[self.noteContent dataUsingEncoding:NSUTF8StringEncoding]; 

阅读

self.noteContent = [[NSString alloc] initWithData:contents encoding:NSUTF8StringEncoding]; 

例子:

- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError **)outError 
{ 

    if ([contents length] > 0) { 
     self.noteContent = [[NSString alloc] initWithData:contents encoding:NSUTF8StringEncoding]; 
    } else { 
     self.noteContent = @""; // When the note is created we assign some default content 
    } 

    [[NSNotificationCenter defaultCenter] postNotificationName:@"noteModified" 
                 object:self];   

    return YES; 

} 

// Called whenever the application (auto)saves the content of a note 
- (id)contentsForType:(NSString *)typeName error:(NSError **)outError 
{ 

    if ([self.noteContent length] == 0) { 
     self.noteContent = @""; 
    } 

    return [self.noteContent dataUsingEncoding:NSUTF8StringEncoding]; 


}