9

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.

ikbal
  • 1,114
  • 1
  • 11
  • 30
  • 1
    Below is the link of Github code: **https://github.com/krishkumar/BlockParty** Have a look at it, may be it can help you out. – jiten Oct 14 '16 at 13:38
  • 1
    @jiten i already use it but it also not working in real device. – ikbal Oct 15 '16 at 04:48
  • The fact that it works in the simulator but not on a device might indicate an entitlement / code signing issue. Make sure that both of your targets (app and extension) have App Groups turned on and declare membership in the correct group. – Jiri Oct 16 '16 at 10:15
  • @Jiri without app group its not work in simulator app group(ON) is compulsory for both simulator and real device i also test it. – ikbal Oct 17 '16 at 09:11

1 Answers1

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)")
            }
        }
    }
}
Jack Goossen
  • 799
  • 4
  • 14