簡體   English   中英

如何知道從WKWebView截獲的請求來自哪個WKWebView實例?

[英]How to know the request intercepted from WKWebView is from which WKWebView instance?

我想為WKWebView所有資源請求(例如圖像和視頻元素)添加引薦來源網址,WebViews會為我發送這些請求。

我已經知道如何鈎住請求,使用WKBrowsingContextController registerSchemeForCustomProtocol: NSURLProtocol-WebKitSupport來攔截請求,然后將引用添加到請求中。

但是有幾個Web視圖,我需要使用Web視圖作為引用,例如:

NSString *referer = [NSString stringWithFormat:@"https://example.com/%@/232/page-frame.html", @"webView1"];

它是動態的。

static NSString* const FilteredKey = @"FilteredKey";

@implementation MyURLProtocol

+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
    return [NSURLProtocol propertyForKey:FilteredKey inRequest:request] == nil;
}

+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
    NSMutableURLRequest *mutableReqeust = [request mutableCopy];
    [mutableReqeust setValue:@"https://example.com/something/232/page-frame.html" forHTTPHeaderField:@"Referer"];
    return [request copy];
}

- (void)startLoading {
    // ...
}

- (void)stopLoading {
    // ...
}

@end

所以我的問題是:我將如何確定哪個Web視圖保存了請求?

您可以為每個Web視圖設置tag以用作其標識符。

然后,在下面的WKWebView delegate方法中:

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    \\ This delegate method is called when web content begins to load in a web view.

    if (webView === self.webView1) {

    } else if (webView === self.webView2) {

    } else if (webView === self.webView3) {

    } else {

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM