How to add or delete rule of safari content block list at run time

735 views Asked by At

I am using in my project safari content blocker extension. when i set the rule in blockerList.json file statically and run the project every thing is working fine. Now i want to set my rule dynamically using the technic as it describes in below.

Guys please help me out to set the rule dynamically at run time.

I try this but i am getting an error when

  1. load from viewcontroller class
fileprivate func saveRuleFile() {

        let ruleList = [["trigger":["url-filter": ".*"],"action":["type": "block"]]]

        let encoder = JSONEncoder()
        encoder.outputFormatting = .prettyPrinted
        if let encoded = try? encoder.encode(ruleList) {

            let sharedContainerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.****.***")
            print("sharedContainerURL = \(String(describing: sharedContainerURL))")

            if let json = String(data: encoded, encoding: .utf8) {
                print(json)
            }

            if let destinationURL = sharedContainerURL?.appendingPathComponent("Rules.json") {
                do {
                    try  encoded.write(to: destinationURL)
                } catch {
                    print (error)
                }
            }
        }
    }
  1. And write this in ContentBlockerRequestHandler class
func beginRequest(with context: NSExtensionContext) {
 let sharedContainerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.****.***")
        let sourceURL = sharedContainerURL?.appendingPathComponent("Rules.json")
        let ruleAttachment = NSItemProvider(contentsOf: sourceURL)
        let item = NSExtensionItem()
        item.attachments = ([ruleAttachment] as! [NSItemProvider])
        context.completeRequest(returningItems: [item], completionHandler: nil)
    }
  1. i try to load using
SFContentBlockerManager.reloadContentBlocker(withIdentifier: "com.app.*****", completionHandler: {(error) in
            if error != nil{
                print("error: \(error.debugDescription)")
            }
        })

when try to execute 3rd number block at run time i'm getting an error. But i go to the file path and checked the json is absolutely fine, its a valid json there.

Error Domain=WKErrorDomain Code=2 "(null)" UserInfo={NSHelpAnchor=Rule list compilation failed: Failed to parse the JSON String.}
1

There are 1 answers

2
Nikita Antonenko On

Try to use JSONSerialization. It work great for me :)

fileprivate func saveRuleFile() {

    let ruleList = [["trigger":["url-filter": ".*"],"action":["type": "block"]]]

    let jsonAsData = try! JSONSerialization.data(withJSONObject: ruleList)

    let sharedContainerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.****.***")
    print("sharedContainerURL = \(String(describing: sharedContainerURL))")

    if let destinationURL = sharedContainerURL?.appendingPathComponent("Rules.json") {
        do {
            try  jsonAsData.write(to: destinationURL)
        } catch {
            print (error)
        }
    }

}