Facing issues with ContentBlockerRequestHandler

Hello guys,

I am currently working on a safari app extension that blocks content. I want the user to configure the rule (turning a rule on and off). Since I can’t overwrite the bundled JSON files and we can’t write to the documents folder, as it’s not accessible to the extension I decided to use App Groups. My approach looks like this:

  • Within the ContentBlockerRequestHandler I want to save the blockerList.json into the app group (Only when launched for the first time)
  • When this is done I want that the handler reads from the app group by taking the url of my json which is within the app group instead of taking the default json in the extension

Since I can not debug the handler I don't know if I am on the right path. The following shows my code:

/ /

class ContentBlockerRequestHandler: NSObject, NSExtensionRequestHandling {

    func beginRequest(with context: NSExtensionContext) {

        

        guard let rulesUrl = loadRules() else {

            let clonedRules = cloneBlockerList()

            save(rules: clonedRules)

            return

        }

        

        guard let attachment = NSItemProvider(contentsOf: rulesUrl) else { return }

//        let attachment = NSItemProvider(contentsOf: Bundle.main.url(forResource: "blockerList", withExtension: "json"))!

        

        let item = NSExtensionItem()

        item.attachments = [attachment]

        

        context.completeRequest(returningItems: [item], completionHandler: nil)

    }

    

    private func cloneBlockerList() -> [Rule] {

        

        var rules: [Rule] = []

        if let url = Bundle.main.url(forResource: "blockerList", withExtension: "json") {

            do {

                let data = try Data(contentsOf: url)

                let decoder = JSONDecoder()

                let jsonData = try decoder.decode(ResponseData.self, from: data)

                rules = jsonData.rules

            } catch {

                print("error:(error)")

            }

        }

        

        return rules

    }

    

    private func save(rules: [Rule]) {

        

        let documentsDirectory = FileManager().containerURL(forSecurityApplicationGroupIdentifier: "my group identifier")

        let archiveURL = documentsDirectory?.appendingPathComponent("rules.json")

        let encoder = JSONEncoder()

        if let dataToSave = try? encoder.encode(rules) {

        do {

            try dataToSave.write(to: archiveURL!)

            } catch {

            // TODO: ("Error: Can't save Counters")

            return;

            }

        }

    }

    

    private func loadRules() -> URL? {

        

        let documentFolder = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "my group identifier")

        guard let jsonURL = documentFolder?.appendingPathComponent("rules.json") else {

            return nil

        }

        

        return jsonURL

    }

}

Thankful for any help

It seems there are few mistakes in this code. Can you try to check some other codes available at Git Hub with Manifest 3 version.

Facing issues with ContentBlockerRequestHandler
 
 
Q