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

发布于 2025-01-06 14:35:04 字数 5942 浏览 0 评论 0原文

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

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

I'm new to iPhone Development.

I have integrated iCloud storage in my application. I am successful in uploading documents on iCloud.

My document's size is around 126799 bytes. During uploading on iCloud I have made sure that a proper document is uploaded on iCloud by printing its length and content on the console. But when I am fetching document from iCloud it only gives me 3/4 of the content of that document. I have also checked this on console by printing its length and content.

/////====== 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

Can you please tell me whats can be the problem? Any suggestion will be appreciated. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉徒眸意 2025-01-13 14:35:04

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

您应该用于

写作

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


}

I got a same problem like your before.

You should use

for writing

[self.noteContent dataUsingEncoding:NSUTF8StringEncoding];

for reading

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

Example :

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


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文