content blocker works in simulator but does not wo

2019-06-16 06:58发布

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条回答
Animai°情兽
2楼-- · 2019-06-16 07:56

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)")
            }
        }
    }
}
查看更多
登录 后发表回答