-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathContentBlockerRequestHandler.swift
69 lines (57 loc) · 2.6 KB
/
ContentBlockerRequestHandler.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// ContentBlockerRequestHandler.swift
// Confirmed Blocker
//
// Copyright © 2019 Confirmed Inc. All rights reserved.
//
import UIKit
import MobileCoreServices
class ContentBlockerRequestHandler: NSObject, NSExtensionRequestHandling {
func attachmentForSettings() -> NSItemProvider {
var blockArray = [] as? Array<String>
if let defaults = UserDefaults(suiteName: "group.com.confirmed") {
if defaults.object(forKey: "AdBlockingEnabled") != nil {
if defaults.bool(forKey: "AdBlockingEnabled") {
blockArray?.append("adBlockList")
blockArray?.append("adBlockListTwo")
//blockArray?.append("adBlockListThree")
}
}
else { //include if key is not initialized
blockArray?.append("adBlockList")
blockArray?.append("adBlockListTwo")
//blockArray?.append("adBlockListThree")
}
if defaults.object(forKey: "PrivacyBlockingEnabled") != nil {
if defaults.bool(forKey: "PrivacyBlockingEnabled") {
blockArray?.append("privacyBlockList")
}
}
else { //include if key is not initialized
blockArray?.append("privacyBlockList")
}
if defaults.object(forKey: "SocialBlockingEnabled") != nil {
if defaults.bool(forKey: "SocialBlockingEnabled") {
blockArray?.append("socialBlockList")
}
}
else { //include if key is not initialized
blockArray?.append("socialBlockList")
}
}
var finalList = [NSDictionary]()
for blockerList in blockArray! {
let data = try! Data(contentsOf: Bundle.main.url(forResource: blockerList, withExtension: "json")!)
let obj = try! JSONSerialization.jsonObject(with: data, options: []) as! [NSDictionary]
finalList.append(contentsOf: obj)
}
let finalListJSON = try! JSONSerialization.data(withJSONObject: finalList, options: JSONSerialization.WritingOptions(rawValue: 0))
let attachment = NSItemProvider(item: finalListJSON as NSSecureCoding?, typeIdentifier: kUTTypeJSON as String)
return attachment
}
func beginRequest(with context: NSExtensionContext) {
let item = NSExtensionItem()
item.attachments = [attachmentForSettings()]
context.completeRequest(returningItems: [item], completionHandler: nil)
}
}