zoukankan      html  css  js  c++  java
  • UIWebView to view self signed websites (No private api, not NSURLConnection)

    What it actually does is to intercept the UIWebView to launch a NSURLConnection to allow the server to be authenticated, therefore then continue the connection using UIWebView, and cancels out the NSURLConnection. Reason so is because after authorising the server once, the rest of the continuous connection would not be blocked. Hope i'm clear. :)

    Finally I got it!

    What you can do is this:

    Initiate your request using UIWebView as normal. Then - in webView:shouldStartLoadWithRequest - we reply NO, and instead start an NSURLConnection with the same request.

    Using NSURLConnection, you can communicate with a self-signed server, as we have the ability to control the authentication through the extra delegate methods which are not available to a UIWebView. So using connection:didReceiveAuthenticationChallenge we can authenticate against the self signed server.

    Then, in connection:didReceiveData, we cancel the NSURLConnection request, and start the same request again using UIWebView - which will work now, because we've already got through the server authentication :)

    Here are the relevant code snippets below.

    Note: Instance variables you will see are of the following type: 
    UIWebView *_web
    NSURLConnection *_urlConnection
    NSURLRequest *_request

    (I use an instance var for _request as in my case it's a POST with lots of login details, but you could change to use the request passed in as arguments to the methods if you needed.)

    #pragma mark - Webview delegate
    
    // Note: This method is particularly important. As the server is using a self signed certificate,
    // we cannot use just UIWebView - as it doesn't allow for using self-certs. Instead, we stop the
    // request in this method below, create an NSURLConnection (which can allow self-certs via the delegate methods
    // which UIWebView does not have), authenticate using NSURLConnection, then use another UIWebView to complete
    // the loading and viewing of the page. See connection:didReceiveAuthenticationChallenge to see how this works.
    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
    {
        NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authenticated);
    
        if (!_authenticated) {
            _authenticated = NO;
    
            _urlConnection = [[NSURLConnection alloc] initWithRequest:_request delegate:self];
    
            [_urlConnection start];
    
            return NO;
        }
    
        return YES;
    }
    
    
    #pragma mark - NURLConnection delegate
    
    - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
    {
        NSLog(@"WebController Got auth challange via NSURLConnection");
    
        if ([challenge previousFailureCount] == 0)
        {
            _authenticated = YES;
    
            NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
    
            [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
    
        } else
        {
            [[challenge sender] cancelAuthenticationChallenge:challenge];
        }
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
    {
        NSLog(@"WebController received response via NSURLConnection");
    
        // remake a webview call now that authentication has passed ok.
        _authenticated = YES;
        [_web loadRequest:_request];
    
        // Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!)
        [_urlConnection cancel];
    }
    
    // We use this method is to accept an untrusted site which unfortunately we need to do, as our PVM servers are self signed.
    - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
    {
        return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
    }

  • 相关阅读:
    博客园美化-SimpleMemor
    Java多线程-synchronized与ReentrantLock
    springboot中删除@SessionAttributes注解的属性
    SSM整合笔记
    Spring中xml和注解方式使用AOP
    Mysql 数据库基本操作
    Mysql 二进制包安装
    named piped tcp proxy 下载
    docker容器中日志文件过大处理方法
    自动做bond的脚本
  • 原文地址:https://www.cnblogs.com/lisa090818/p/4276023.html
Copyright © 2011-2022 走看看