【问题标题】:fetching document from iCloud giving me 3/4 content of that document not the full content从 iCloud 获取文档给我该文档的 3/4 内容而不是完整内容
【发布时间】:2012-02-18 06:18:22
【问题描述】:

我是 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

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

【问题讨论】:

  • 您可能需要展示一些代码才能获得解决方案。
  • 我已经添加了代码。请检查一下,让我知道代码有什么问题。谢谢
  • 我看到的唯一“明显”的事情就在最后,您正在使用来自 utf8 字符串的字节和 noteContent 的长度执行 [NSData dataWithBytes:length:]。 UTF-8 的字节长度不一定与 NSString 的长度相同。除此之外,遗憾的是,在调试器中发现这个问题可能比阅读代码更容易。
  • 感谢您的回复。我还没有得到解决方案。我该如何解决这个问题?长度不是问题,但至少整个文本文档内容应该对我可见,但我没有得到它。请以其他方式帮助我。

标签: iphone ios ipad document icloud


【解决方案1】:

我遇到了和你以前一样的问题。

你应该使用

写作

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


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    • 2017-08-13
    • 2022-01-05
    相关资源
    最近更新 更多