WKWebView and NSURLProtocol not working

后端 未结 4 1786
深忆病人
深忆病人 2020-12-02 08:05

When using the old UIWebView you could catch the requests by implementing a custom NSURLProtocol. I us this to handle requests that requires authentication.

I tried

4条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-02 08:22

    WKWebView has a navigationDelegate property. If that delegate is set WKWebView will call the didReceiveAuthenticationChallenge method on that delegate if the method is implemented. You need to place your authentication code in this method. Example:

    - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler {
        NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:@"bob"
                                                                   password:@"pass"
                                                                persistence:NSURLCredentialPersistenceNone];
        completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
    }
    

提交回复
热议问题