Reputation: 1084
I've built a Safari Content Blocker Extension which works on iOS, tested on simulator iOS 17.5, but which doesn't work on macOS Sonoma 14.6. It's hard to create a minimal reproducible example, since there are so much build settings required for these extensions, but here is the code. Please let me know if you can find anything wrong with this.
The error on macOS is:
Error Domain=WKErrorDomain Code=6 "(null)" UserInfo={NSHelpAnchor=Rule list parsing failed: Failed to parse the JSON String.}
The Content Blocker:
import Foundation
class ContentBlockerRequestHandler: NSObject, NSExtensionRequestHandling {
func beginRequest(with context: NSExtensionContext) {
let attachment = NSItemProvider(object: String(
"""
[
{
"trigger": {
"url-filter": "facebook.com"
},
"action": {
"type": "block"
}
}
]
"""
) as NSString)
let item = NSExtensionItem()
item.attachments = [attachment]
context.completeRequest(returningItems: [item], completionHandler: nil)
}
}
The app:
import SwiftUI
import SafariServices
@main
struct MyApp: App {
let contentBlockerIdentifier = Bundle.main.bundleIdentifier!
+ ".ContentBlocker"
init() {
SFContentBlockerManager.reloadContentBlocker(withIdentifier: contentBlockerIdentifier,
completionHandler: { (error) in
if let error = error {
print(error) // the error is printed here
}
})
}
var body: some Scene {
WindowGroup {
Text("Hello")
}
}
}
Upvotes: 0
Views: 74