我正在创建一个应用,其中WKWebView
中已加载supremenewyork.com个网站。但是,在加载网站时,我不想在其中加载图像。如何在Objective-C中做到这一点。
答案 0 :(得分:2)
我有以下阻止在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];
});
}
}];
}