禁止在 WKWebView ios 中加载图像

Jag*_*ngh 2 objective-c ios wkwebview

我正在创建一个应用程序,其中Supremenewyork.com网站以WKWebView. 但是当网站加载时我不想加载图像。我怎样才能在 Objective-C 中做到这一点?

ash*_*ish 5

我有以下内容阻止在 WKWebView 中加载网站图像。我使用了苹果官方网站上记录的内容拦截器规则。检查这里。创建内容拦截器

- (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];
            });
        }
    }];
}
Run Code Online (Sandbox Code Playgroud)

输出: 1。 禁用图像加载 1

输出: 2。 禁用图像加载 2