【问题标题】:How to load a webpage (Https using a self signed certificate) on the UIWebView?如何在 UIWebView 上加载网页(使用自签名证书的 Https)?
【发布时间】:2011-12-09 23:45:40
【问题描述】:

如何在 UIWebView (iOS) 上加载网页(使用自签名证书的 Https)?我尝试使用 NSURLConnection 并且能够将 NSMutableData 加载到 UIwebView。但在这种情况下,我无法看到该页面中的图像。

【问题讨论】:

    标签: iphone objective-c ios


    【解决方案1】:

    自签名证书大部分时间无法被客户端验证,这就是客户端无法加载数据的原因,因为它无法从服务器验证。

    证书使用域来验证自身,因此在开发环境中使用起来有点困难。

    有一个为本地环境证书创建的工具,我不确定它是如何在内部工作的,我最好的想法是它在您的环境中添加了另一个 CA 文件。

    您可能想检查mkcert 这是非常容易使用的好方法。

    【讨论】:

      【解决方案2】:
      [yourWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"yourUrlGoesHere"]]];
      

      关于证书部分,如果您的 URL 可在 Safari 中打开,它将在 web 视图中打开。


      评论更新

      试试下面的

      NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"yourUrlGoesHere"]];
      NSURLConnection *urlConnection=[NSURLConnection connectionWithRequest:req delegate:self];
      [yourWebView loadRequest:req];
      

      并在您的类中实现以下委托方法,

      #pragma mark - NSURLConnection Delegate Methods
      - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
          return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
      }
      
      - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
      
          [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
      }
      

      【讨论】:

      • 当我尝试在 Safari 中打开 URL 时,我会收到一条错误消息,指出进入不安全。如果我点击继续,我会转到网页。我正在使用自签名证书进行 HTTPS 调用。
      • 我试过这个方法。我从响应中获取 NSMutable 数据。但是当我尝试将其加载到 webview 时,我无法在网页中看到图像。我正在发布示例代码。如果我遗漏了什么,请告诉我。**- (void) connectionDidFinishLoading:(NSURLConnection )connection { [self.k_webView loadData:data MIMEType:mimeType textEncodingName: @"UTF-8" baseURL:nil]; }*
      猜你喜欢
      • 2014-11-11
      • 1970-01-01
      • 2020-06-16
      • 2017-02-19
      • 1970-01-01
      • 2014-06-22
      • 2012-01-16
      • 2016-01-12
      • 2016-12-29
      相关资源
      最近更新 更多