内容拦截器在模拟器中工作,但在iPhone设备中不起作用

ikb*_*bal 9 safari-extension ios swift safari-content-blocker

我正在研究内容拦截器并阻止成人网站所以,这个代码是完美的模拟器工作,当我在iPhone 6上测试然后它的无人网站是阻止

Alamofire.request(url).responseJSON { response in
            if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
                print("Data: \(utf8Text)")

                self.containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.domainName.contentBlocker")
                let path = self.containerURL?.appendingPathComponent("blockerList.json")

                self.text = utf8Text.description
                do {

                    try self.text.write(to: path!, atomically: true, encoding: String.Encoding.utf8)
                }catch{
                    print(error)
                }
                print(path)

            }
        }
Run Code Online (Sandbox Code Playgroud)

然后在扩展处理程序文件上加载数据后.提前致谢.

Jac*_*sen 1

我遇到了类似的问题,直接在模拟器上读取组的内容,但在设备上不行。它通过在组中创建一个子目录并在该子目录中进行所有读写操作来解决此问题。

if let root = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "YOUR_GROUP_NAME") {
    let url = root.appendingPathComponent("storage")
    try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)

    let fileCoordinator = NSFileCoordinator()
    var error: NSError?
    fileCoordinator.coordinate(readingItemAt: url, options: .withoutChanges, error: &error) { url in

        if let urls = try? FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: [.canonicalPathKey], options: []) {
            for u in urls {
                 print("\(u.standardizedFileURL)")
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)