Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 623
ikbal Avatar asked Oct 05 '16 05:10

ikbal


People also ask

How do I show my Iphone simulator in Xcode?

To launch a Simulator without running an appChoose Xcode > Open Developer Tool > Simulator. Control-click the Xcode icon in the Dock, and from the shortcut menu, choose Open Developer Tool > Simulator.

How do you choose a simulated device as a destination?

Select a simulated device For iOS, tvOS, and watchOS apps, you can choose a simulated device, under [Platform] Simulators, from the run destination menu next to the scheme menu in the toolbar.


1 Answers

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)")
            }
        }
    }
}
like image 187
Jack Goossen Avatar answered Oct 13 '22 00:10

Jack Goossen