content blocker works in simulator but does not work in iPhone device

三世轮回 提交于 2019-12-08 16:25:52

问题


i am working on content blocker and block the adult site so, this code is perfect work on simulator when i test on iPhone 6 then its no one site is block

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)

            }
        }

Then after load data on extension handler file. Thanks in advance.


回答1:


I had a similar issue where reading the contents of the group directly worked on the simulator, but not on a device. It resolved this by creating a subdirectory in the group and do all reading writing in this subdirectory.

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)")
            }
        }
    }
}


来源:https://stackoverflow.com/questions/39865806/content-blocker-works-in-simulator-but-does-not-work-in-iphone-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!