content blocker works in simulator but does not wo

2019-06-16 06:59发布

问题:

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