在iOS Content Blocker中使用多个JSON列表

时间:2017-05-07 01:43:37

标签: ios json swift safari-content-blocker

我正在尝试在iOS上创建自己的内容拦截器。我想为不同类型的内容(跟踪,广告,成人网站等)创建单独的json列表。我遇到了这个https://github.com/calebhicks/ios-safari-content-blocking,它声明你可以创建一个“附件”数组,而不是依赖于单个“blockerList”json文件。

func beginRequest(with context: NSExtensionContext) {
    var jsonFiles:Array<NSItemProvider> = Array()

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

    let attachment2 = NSItemProvider(contentsOf: Bundle.main.url(forResource: "testList", withExtension: "json"))!
    jsonFiles.append(attachment2)

    let item = NSExtensionItem()
    item.attachments = jsonFiles

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

此代码的大部分是Content Blocker Extension设置的默认代码,但我添加的是附加了attachment和attachment2的jsonFiles数组。运行时,只加载了两个规则集中的一个,从不两者的结合。关于为什么只加载一个规则集的任何想法?

1 个答案:

答案 0 :(得分:0)

您可以将两个JSON规则文件合并为一个文件,然后使用该文件。

    import UIKit
        import MobileCoreServices
        class ContentBlockerRequestHandler: NSObject, NSExtensionRequestHandling {

        func beginRequest(with context: NSExtensionContext) {

        let sharedContainerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "you app group identifier")

                let sourceURLRules = sharedContainerURL?.appendingPathComponent("Rules1.json")
                let sourceURLRules2 = sharedContainerURL?.appendingPathComponent("Rules2.json")
                do {
                    let jsonDecoder = JSONDecoder()

                    let dataFormRules1 = try Data(contentsOf: sourceURLRules1!, options: .mappedIfSafe)// Rule is Decode able Swift class            
                   let  rulesArray1 = try? jsonDecoder.decode(Array<Rule>.self,from: dataFormRules1)

                    let dataFormRules2 = try Data(contentsOf: sourceURLRules2!, options: .mappedIfSafe)
                    let  rulesArray2 = try? jsonDecoder.decode(Array<Rule>.self,from: dataFormRules2)

                    saveCombinedRuleFile(ruleList: rulesArray1! + rulesArray2!)

                } catch {
                    //handle error condition
                }

                let sourceURLCombinedRule = sharedContainerURL?.appendingPathComponent("CombinedRule.json")
                let combinedRuleAttachment = NSItemProvider(contentsOf: sourceURLCombinedRule)
                let item = NSExtensionItem()
                item.attachments = [combinedRuleAttachment]
                context.completeRequest(returningItems: [item], completionHandler: nil)
            }

            func saveCombinedRuleFile(ruleList:[Rule]) {
                let encoder = JSONEncoder()
                if let encoded = try? encoder.encode(ruleList) {
                    let sharedContainerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.slashnext.mobileshield")
                    if let json = String(data: encoded, encoding: .utf8) {
                        print(json)
                    }
                    if let destinationURL = sharedContainerURL?.appendingPathComponent("CombinedRule.json") {
                        do {
                            try  encoded.write(to: destinationURL)
                        } catch {
                            print ("catchtry")
                        }
                    }
                }
            }
        }