WKWebView does not load https URL?

前端 未结 6 551
予麋鹿
予麋鹿 2021-02-07 00:16

I have a WKWebView which should load the following url:

https://buchung.salonmeister.de/place/#offer-details-page?id=907599&venueId=301655

相关标签:
6条回答
  • 2021-02-07 00:56
    // Add plist file 
    <dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>google.com</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
    

    if WKWebView not support then declare .m file below code:

    @interface WebScannerViewController()
    {
    
     WKWebView *webView;
    
    }
    
    
    @end
    
    
    
    @implementation WebScannerViewController
    
     - (void)viewDidLoad   {
    
        [super viewDidLoad];
        webView.hidden=YES;
    
        webView.UIDelegate = self;
        webView.navigationDelegate = self;
        self.loadingSign.hidden = NO;
    
    
            webView.frame=CGRectMake(0, 94, Width, Height-128);
        }
    
    0 讨论(0)
  • 2021-02-07 01:05

    For me, the issue was caused by server trust check from the WKWebView.

    To fix this I had to handle the challenge authentication callback and return a server trust credential.

    Swift 4

    func webView(_ webView: WKWebView, 
        didReceive challenge: URLAuthenticationChallenge, 
        completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) 
    {
        if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust)
        {
            let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
            completionHandler(.useCredential, cred)
        }
        else
        {
            completionHandler(.performDefaultHandling, nil)
        }
    }
    
    0 讨论(0)
  • If you are in a sandboxed macOS app, you'll need to set the Outgoing Connections (Client) capability, you won't need to mess with Allow Abitrary Loads which shouldn't come into play for trusted https requests.

    For iOS however, allowing client connections is the default, so you may need to implement WKNavigationDelegate methods to handle security. Make sure you aren't just trusting untrusted certificates though. This Swift Talk video from objc.io is the best resource I know of, definitely worth 20 minutes if you're working in this area: https://talk.objc.io/episodes/S01E57-certificate-pinning

    0 讨论(0)
  • 2021-02-07 01:10

    not sure if the same error reason, but the problem was the same for me under iOS9

    some domains couldn't be loaded

    turned out that the problem was in

    - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler {
    

    and providing back

    completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
    

    where I should have returned

    completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
    

    I was using WRONG code from https://github.com/ShingoFukuyama/WKWebViewTips

    0 讨论(0)
  • 2021-02-07 01:15

    Add this to your plist

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
    

    Here's the explanation for this change in 9.0 http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/

    Also if you want to set it up more secure it gives you a more complex way to do that.

    0 讨论(0)
  • 2021-02-07 01:16

    I had a similar problem with a site that was also protected with a high security TLS 1.2 certificate. To get the WKWebView to accept the server's certificate, I added this code to my web view controller delegate:

    -(void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
    {
        if ([[[challenge protectionSpace]authenticationMethod] isEqualToString: @"NSURLAuthenticationMethodServerTrust"]) {
            SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
            CFDataRef exceptions = SecTrustCopyExceptions(serverTrust);
            SecTrustSetExceptions(serverTrust, exceptions);
            CFRelease(exceptions);
            newCredential = [NSURLCredential credentialForTrust:serverTrust];
            completionHandler(NSURLSessionAuthChallengeUseCredential, newCredential);
        } else {
            completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, newCredential);
        }
    }
    
    0 讨论(0)
提交回复
热议问题