【问题标题】:NSDictionary loading a NSURL with URLByResolvingBookmarkDataNSDictionary 使用 URLByResolvingBookmarkData 加载 NSURL
【发布时间】:2014-06-28 08:49:01
【问题描述】:

我现在正在尝试 Bookmark Data 解决方案,我重新调用了一个 NSURL,但它不起作用。NSURL 的格式正确,但是当我使用它来创建字典或字符串时,这些都是 nil。 我使用的代码是这样的:

- (NSData *)bookmarkFromURL:(NSURL *)url 
{ NSError *error = nil; NSData *bookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
             includingResourceValuesForKeys:NULL
                              relativeToURL:NULL
                                      error:&error];
if (error) {
NSLog(@"Error creating bookmark for URL (%@): %@", url, error);
[NSApp presentError:error];
}

 return bookmark;
}

- (NSURL *)urlFromBookmark:(NSData *)bookmark {
NSURL *url = [NSURL URLByResolvingBookmarkData:bookmark
                                   options:NSURLBookmarkResolutionWithSecurityScope
                             relativeToURL:NULL
                       bookmarkDataIsStale:NO
                                     error:NULL];
return url;
}

【问题讨论】:

    标签: cocoa nsdictionary sandbox nsurl


    【解决方案1】:

    下面是使用 NSURLBookmarkCreationWithSecurityScope 为 Sandbox Cocoa App 保存和检索书签的正确技巧: 终于

    -(void)saveTheNSData:(NSData *)data withFileName:(NSString *)fileName
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
    
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
    
        [data writeToFile:filePath atomically:NO];
    }
    
    -(NSURL*) getNSURLFromBookmarkIfExists:(NSString*) filename forType:(NSString*) type
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
    
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:filename];
    
        NSData* data = [[NSData alloc] initWithContentsOfFile: filePath];
        if(data != nil)
        {
            NSURL* outUrl = [NSURL URLByResolvingBookmarkData:[[NSUserDefaults standardUserDefaults] objectForKey:type] options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:nil error:nil];
            [outUrl startAccessingSecurityScopedResource];
            return outUrl;
        }
        else
            return nil;
    }
    

    还请查看 此示例,了解使用 [NSUserDefaults standardUserDefaults] 设置书签的正确方法:

    -(void) savePlistWithPlistUrl:(NSURL*) plistUrl andImageUrl:(NSURL*) imageUrl
    {
        NSData *bookmarkPlist = [self bookmarkFromURL:plistUrl];
    
        NSArray* pathSplitted = [pathFilePlist pathComponents];
        NSString* filenamePlist = [pathSplitted objectAtIndex:[pathSplitted count]-1];
    
        NSData *bookmarkImage = [self bookmarkFromURL:imageUrl];
    
        NSArray* pathImageSplitted = [imageUrl pathComponents];
        NSString* filenameImage = [pathImageSplitted objectAtIndex:[pathImageSplitted count]-1];
    
        NSUserDefaults* prefs = [NSUserDefaults standardUserDefaults];
        [prefs setObject:bookmarkPlist forKey:@"ExportPlist"];
        [prefs setObject:bookmarkImage forKey:@"ExportImage"];
        [prefs synchronize];
        // save the data bookmark
        [self saveTheNSData: bookmarkPlist withFileName:filenamePlist];
        [self saveTheNSData: bookmarkImage withFileName:filenameImage];
    }
    

    【讨论】:

    • 完成后别忘了致电stopAccessingSecurityScopedResource。来自 Apple 的 NSURL 文档:“如果您在不再需要文件系统资源时未能放弃对文件系统资源的访问,则您的应用程序会泄漏内核资源。如果泄漏了足够的内核资源,您的应用程序将失去添加文件系统的能力位置到其沙盒,例如通过 Powerbox 或安全范围的书签,直到重新启动。”
    猜你喜欢
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    相关资源
    最近更新 更多