How can document-scoped security bookmarks be stored in Core Data?

时间:2016-08-30 04:31:22

标签: macos core-data nsurl appstore-sandbox security-scoped-bookmarks

I have been making an app that contains several targets cooperate with each other as an app group.

Problems came when I began to make it Sandboxed:

  • the app stores URLs in the Core Data, and one target does the saving and another the reading
  • now the bookmark created by a target cannot be solved by the other

I know document-scoped bookmarks could be shared among apps. But it seems that they need to be stored into another "wrapper" document.

Is there a good way to store document-scoped bookmarks in Core Data and share them among different targets?

1 个答案:

答案 0 :(得分:0)

对于多个目标,请确保使用通用应用程序组容器共享Core数据模型?所有目标应该在同一个容器中查看?还要确保目标是同一Core数据模型的成员。如果不是,则不同的目标不能使用相同的模型。查看核心数据属性区域内的身份检查器,确保勾选目标?

使用以下方式从URL存储安全范围的书签:

(NSData *) bookmarkDataWithOptions: (NSURLBookmarkCreationOptions) options 
    includingResourceValuesForKeys: (NSArray<NSURLResourceKey> *) keys 
                     relativeToURL: (NSURL *) relativeURL 
                             error: (NSError * _Nullable *) error;

NSData保存为核心数据中的Binary Data数据类型。

使用以下方法解析书签:

(instancetype) URLByResolvingBookmarkData: (NSData *)bookmarkData 
                                  options: (NSURLBookmarkResolutionOptions) options
                            relativeToURL: (NSURL *) relativeURL 
                      bookmarkDataIsStale: (BOOL *) isStale
                                    error: (NSError * _Nullable *) error; 

这会返回一个网址。

查看WWDC 15 session 234以及与安全范围书签相关的示例代码。

希望这会有所帮助..