Arm*_*let 3 swift safari-content-blocker
我NSItemProvider(contentsOfURL: NSBundle.mainBundle().URLForResource("blockerList", withExtension: "json")在内容拦截器扩展中使用该功能.
问题是我的所有规则都存储在一些词典中,当我使用这个功能时,总是因为规则已经改变了.我正在从这些字典中创建一个看起来像的字符串"[{\"trigger\": {\"url-filter\": \"webiste.com\"},\"action\": {"\type\": \"css-display-none\",\"selector\":\".testContentBlocker\"}}]",我必须在JSON文件中将其转换为最终能够在前面描述的函数中使用它.
而不是必须将String放在JSON文件中才能使用它,我可以做一些更简单的事情NSItemProvider()吗?
通过在调试器中加载扩展(并使用Hopper),您可以看到NSItemProvider(contentsOfURL:)只是注册以提供文件内容的类型数据public.json.
(lldb) po attachment
<NSItemProvider: 0x7fd4c250f2a0> {types = (
"public.file-url",
"public.json"
)}
Run Code Online (Sandbox Code Playgroud)
它大致相当于:
// possible implementation of NSItemProvider.init(contentsOfURL:)
convenience init?(contentsOfURL fileURL: NSURL!)
{
self.init(item: fileURL, typeIdentifier: (fileURL.fileURL ? kUTTypeFileURL : kUTTypeURL) as String)
let type = UTTypeCreatePreferredIdentifierForTag(
kUTTagClassFilenameExtension, fileURL.pathExtension!, nil)?.takeRetainedValue() as! String
registerItemForTypeIdentifier(type) { completionHandler, expectedValueClass, options in
let data = try! NSData(contentsOfURL: fileURL, options: .DataReadingMappedAlways)
completionHandler(data, nil)
}
}
Run Code Online (Sandbox Code Playgroud)
所以你可以自己在内存中这样做:
// get the data
let data = NSData(contentsOfURL: NSBundle.mainBundle().URLForResource("blockerList", withExtension: "json")!)
// put the data in an item provider
let attachment = NSItemProvider(item: data, typeIdentifier: kUTTypeJSON as String)
// send the item to Safari
let item = NSExtensionItem()
item.attachments = [attachment]
context.completeRequestReturningItems([item], completionHandler: nil);
Run Code Online (Sandbox Code Playgroud)
如果要动态提供内容,可以使用NSJSONSerialization在运行时将字典转换为NSData.
| 归档时间: |
|
| 查看次数: |
854 次 |
| 最近记录: |