从iCloud中获取文档,为我提供该文档的3/4内容而不是完整内容

时间:2012-02-18 06:18:22

标签: iphone ios ipad document icloud

我是iPhone开发的新手。

我已在我的应用程序中集成了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

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

1 个答案:

答案 0 :(得分: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];


}