在iOS内容拦截器中使用多个JSON列表

在iOS内容拦截器中使用多个JSON列表,ios,json,swift,safari-content-blocker,Ios,Json,Swift,Safari Content Blocker,我正在尝试在iOS上创建自己的内容拦截器。我想为不同类型的内容(跟踪、广告、成人网站等)创建单独的json列表。我遇到了这样一个问题,即您可以创建一个“附件”数组,而不是依赖单一的“blockerList”json文件 func beginRequest(with context: NSExtensionContext) { var jsonFiles:Array<NSItemProvider> = Array() let attachment = NSItemPr

我正在尝试在iOS上创建自己的内容拦截器。我想为不同类型的内容(跟踪、广告、成人网站等)创建单独的json列表。我遇到了这样一个问题,即您可以创建一个“附件”数组,而不是依赖单一的“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)
}
func beginRequest(带上下文:NSExtensionContext){
var jsonFiles:Array=Array()
让attachment=NSItemProvider(contentsOf:Bundle.main.url(forResource:“blockerList”,带扩展名:“json”)!
jsonFiles.append(附件)
让attachment2=NSItemProvider(contentsOf:Bundle.main.url(forResource:“testList”,扩展名为:“json”)!
jsonFiles.append(附件2)
let item=NSExtensionItem()
item.attachments=jsonFiles
context.completeRequest(returningItems:[item],completionHandler:nil)
}

这段代码的大部分是Content Blocker扩展设置的默认代码,但我添加的是jsonFiles数组,附件和attachment2放在其中。运行此操作时,只加载两个规则集中的一个,而不加载两个规则集的组合。您知道为什么只加载一个规则集吗?

您可以将两个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: "you app group identifier")
                    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")
                        }
                    }
                }
            }
        }
导入UIKit
进口流动储备
类ContentBlockErrorRequestHandler:NSObject,NSExtensionRequestHandling{
func beginRequest(上下文为:NSExtensionContext){
让sharedContainerURL=FileManager.default.containerURL(用于安全应用程序组标识符:“您的应用程序组标识符”)
让sourceURLRules=sharedContainerURL?.appendingPathComponent(“Rules1.json”)
让sourceURLRules2=sharedContainerURL?.appendingPathComponent(“Rules2.json”)
做{
让jsonDecoder=jsonDecoder()
让dataFormRules1=try Data(contentsOf:sourceURLRules1!,options:.mappedIfSafe)//规则是可解码的Swift类
let rulesArray1=try?jsonDecoder.decode(Array.self,from:dataFormRules1)
让dataFormRules2=尝试数据(内容:sourceURLRules2!,选项:.mappedIfSafe)
let rulesArray2=try?jsonDecoder.decode(Array.self,from:dataFormRules2)
saveCombinedRuleFile(规则列表:规则数组1!+规则数组2!)
}抓住{
//处理错误条件
}
让sourceURLCombinedRule=sharedContainerURL?.appendingPathComponent(“CombinedRule.json”)
让combinedRuleAttachment=NSItemProvider(内容:sourceURLCombinedRule)
let item=NSExtensionItem()
item.attachments=[combinedRuleAttachment]
context.completeRequest(returningItems:[item],completionHandler:nil)
}
func saveCombinedRuleFile(规则列表:[规则]){
let encoder=JSONEncoder()
如果let encoded=try?encoder.encode(规则列表){
让sharedContainerURL=FileManager.default.containerURL(用于安全应用程序组标识符:“您的应用程序组标识符”)
如果让json=String(数据:编码,编码:.utf8){
打印(json)
}
如果让destinationURL=sharedContainerURL?.appendingPathComponent(“CombinedRule.json”){
做{
尝试编码。写入(到:destinationURL)
}抓住{
打印(“catchtry”)
}
}
}
}
}

您可以将两个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: "you app group identifier")
                    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")
                        }
                    }
                }
            }
        }
导入UIKit
进口流动储备
类ContentBlockErrorRequestHandler:NSObject,NSExtensionRequestHandling{
func beginRequest(上下文为:NSExtensionContext){
让sharedContainerURL=FileManager.default.containerURL(用于安全应用程序组标识符:“您的应用程序组标识符”)
让sourceURLRules=sharedContainerURL?.appendingPathComponent(“Rules1.json”)
让sourceURLRules2=sharedContainerURL?.appendingPathComponent(“Rules2.json”)
做{
让jsonDecoder=jsonDecoder()
让dataFormRules1=try Data(contentsOf:sourceURLRules1!,options:.mappedIfSafe)//规则是可解码的Swift类
let rulesArray1=try?jsonDecoder.decode(Array.self,from:dataFormRules1)
让dataFormRules2=尝试数据(内容:sourceURLRules2!,选项:.mappedIfSafe)
let rulesArray2=try?jsonDecoder.decode(Array.self,from:dataFormRules2)
saveCombinedRuleFile(规则列表:规则数组1!+规则数组2!)
}抓住{
//处理错误条件
}
让sourceURLCombinedRule=sharedContainerURL?.appendingPathComponent(“CombinedRule.json”)
让combinedRuleAttachment=NSItemProvider(内容:sourceURLCombinedRule)
let item=NSExtensionItem()
item.attachments=[combinedRuleAttachment]
context.completeRequest(returningItems:[item],completionHandler:nil)
}
func saveCombinedRuleFile(规则列表:[规则]){
let encoder=JSONEncoder()
如果let encoded=try?encoder.encode(规则列表){
让sharedContainerURL=FileManager.default.containerURL(用于安全应用程序组标识符:“您的应用程序组标识符”)
如果让json=String(数据:编码,编码:.utf8){
打印(json)
}
如果让destinationURL=sharedContainerURL?.appendingPathComponent(“CombinedRule.json”){