禁止在 WKWebView 中加载图像 ios
Disable loading images in WKWebView ios
我正在创建一个应用程序,其中 supremenewyork.com 网站在 WKWebView
中加载。但是当网站加载时我不想在其中加载图像。我如何在 Objective-C.
中执行此操作
我有以下阻止在 WKWebView 中加载网站图像的方法。我使用了 Apple 官方网站上记录的内容拦截器规则。在这里检查。 Creating a Content Blocker.
- (void)viewDidLoad {
[super viewDidLoad];
// id blockRules = @" [{ \"trigger\": { \"url-filter\": \".*\", \"resource-type\": [\"image\"] }, \"action\": { \"type\": \"block\" } }, { \"trigger\": { \"url-filter\": \".*\", \"resource-type\": [\"style-sheet\"] }, \"action\": { \"type\": \"block\" } }, { \"trigger\": { \"url-filter\": \".*.jpeg\" }, \"action\": { \"type\": \"ignore-previous-rules\" } }] ";
id blockRules = @" [{ \"trigger\": { \"url-filter\": \".*\", \"resource-type\": [\"image\"] }, \"action\": { \"type\": \"block\" } }] ";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.supremenewyork.com/"]];
[[WKContentRuleListStore defaultStore] compileContentRuleListForIdentifier: @"ContentBlockingRules" encodedContentRuleList:blockRules completionHandler:^(WKContentRuleList *contentRuleList, NSError *error) {
if (error != nil) {
NSLog(@"Error = %@", error.localizedDescription);
}
else {
WKWebViewConfiguration *configuration = self.webView.configuration;
[[configuration userContentController] addContentRuleList:contentRuleList];
dispatch_async(dispatch_get_main_queue(), ^{
[self.webView loadRequest:request];
});
}
}];
}
输出:
1.
输出:
2.
这是基于@Mayur Karmur 回答的 Swift 版本。
let blockRules = "[{ \"trigger\": { \"url-filter\": \".*\", \"resource-type\": [\"image\"] }, \"action\": { \"type\": \"block\" } }]";
WKContentRuleListStore.default().compileContentRuleList(forIdentifier: "ContentBlockingRules", encodedContentRuleList: blockRules) {
contentRuleList, error in
if let error = error {
print("error="+error.localizedDescription);
}else if let contentRuleList = contentRuleList{
let configuration = self.webView.configuration;
configuration.userContentController.add(contentRuleList);
}
}
我正在创建一个应用程序,其中 supremenewyork.com 网站在 WKWebView
中加载。但是当网站加载时我不想在其中加载图像。我如何在 Objective-C.
我有以下阻止在 WKWebView 中加载网站图像的方法。我使用了 Apple 官方网站上记录的内容拦截器规则。在这里检查。 Creating a Content Blocker.
- (void)viewDidLoad {
[super viewDidLoad];
// id blockRules = @" [{ \"trigger\": { \"url-filter\": \".*\", \"resource-type\": [\"image\"] }, \"action\": { \"type\": \"block\" } }, { \"trigger\": { \"url-filter\": \".*\", \"resource-type\": [\"style-sheet\"] }, \"action\": { \"type\": \"block\" } }, { \"trigger\": { \"url-filter\": \".*.jpeg\" }, \"action\": { \"type\": \"ignore-previous-rules\" } }] ";
id blockRules = @" [{ \"trigger\": { \"url-filter\": \".*\", \"resource-type\": [\"image\"] }, \"action\": { \"type\": \"block\" } }] ";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.supremenewyork.com/"]];
[[WKContentRuleListStore defaultStore] compileContentRuleListForIdentifier: @"ContentBlockingRules" encodedContentRuleList:blockRules completionHandler:^(WKContentRuleList *contentRuleList, NSError *error) {
if (error != nil) {
NSLog(@"Error = %@", error.localizedDescription);
}
else {
WKWebViewConfiguration *configuration = self.webView.configuration;
[[configuration userContentController] addContentRuleList:contentRuleList];
dispatch_async(dispatch_get_main_queue(), ^{
[self.webView loadRequest:request];
});
}
}];
}
输出:
1.
输出:
2.
这是基于@Mayur Karmur 回答的 Swift 版本。
let blockRules = "[{ \"trigger\": { \"url-filter\": \".*\", \"resource-type\": [\"image\"] }, \"action\": { \"type\": \"block\" } }]";
WKContentRuleListStore.default().compileContentRuleList(forIdentifier: "ContentBlockingRules", encodedContentRuleList: blockRules) {
contentRuleList, error in
if let error = error {
print("error="+error.localizedDescription);
}else if let contentRuleList = contentRuleList{
let configuration = self.webView.configuration;
configuration.userContentController.add(contentRuleList);
}
}