【问题标题】:How to call https url in uiwebview?如何在uiwebview中调用https url?
【发布时间】:2013-12-20 08:57:35
【问题描述】:

我有一个 HTTPS 网址。它正在 iPhone Safari 中加载,但在 UIWebView 中不起作用。

错误是:

NSURLConnection/CFURLConnection HTTP 加载失败 (kCFStreamErrorDomainSSL, -9813)

我该如何解决这个问题?

【问题讨论】:

  • 您的问题是如何解决的?你用过@Durai Amuthan 的答案吗?如果是,那么您是如何将数据加载到 webview 中的?

标签: ios ssl uiwebview


【解决方案1】:

我在下面解释了如何在 UIWebview 中访问 https url,它将帮助您解决问题,因为它对我有用。

调用http与https url相同。

但是,如果您使用的是自签名证书,则有必要添加一些额外的代码。因为默认情况下 iOS 将拒绝所有不受信任的 https 连接。

限制不受信任的连接是非常好的默认行为,任何禁用此行为都有很高的风险。因此,我们将在绕过默认行为时显示警报。

-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace;

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;

以上这两种方法允许我们为可信连接提供我们自己的身份验证机制

#import "ClassCon.h"
//  For now, I've hard coded the IP address of my trusted server.
#define TRUSTED_HOST @"192.168.1.2"


@implementation ClassCon {
    NSMutableData *contentData;
    NSURLConnection *conn;
}

-(void) loadContent {
    contentData = [NSMutableData data];
    NSString *contentURL = @"our url";
    conn = [[NSURLConnection alloc] initWithRequest:
            [NSURLRequest requestWithURL:[NSURL URLWithString:contentURL]] delegate:self];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [contentData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Bad: %@", [error description]);
    ContentResponse *response = [[ContentResponse alloc]initWithRc:-999 andDescr:@"error" andAction:0];


    conn = nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        NSString *loadedContent = [[NSString alloc] initWithData:
                                     contentData encoding:NSUTF8StringEncoding];
        NSLog(@"Loaded content: %@",loadedContent);

    }

// ------------ ByPass ssl starts ----------
-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:
(NSURLProtectionSpace *)protectionSpace {
    return [protectionSpace.authenticationMethod
            isEqualToString:NSURLAuthenticationMethodServerTrust];
}

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:
(NSURLAuthenticationChallenge *)challenge {
    if (([challenge.protectionSpace.authenticationMethod
          isEqualToString:NSURLAuthenticationMethodServerTrust])) {
        if ([challenge.protectionSpace.host isEqualToString:TRUSTED_HOST]) {
            NSLog(@"Allowing bypass...");
            NSURLCredential *credential = [NSURLCredential credentialForTrust:
                                           challenge.protectionSpace.serverTrust];
            [challenge.sender useCredential:credential
                 forAuthenticationChallenge:challenge];
        }
    }
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
// -------------------ByPass ssl ends




@end

希望对你有帮助

【讨论】:

  • @Duraiamuthan - 您的代码中的 webveiw 在哪里?你能用 webview 解释一下代码吗?
  • 我们必须使用钛把上面的代码放在哪里。
猜你喜欢
  • 2011-09-12
  • 1970-01-01
  • 2016-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-15
  • 2019-03-01
  • 2021-04-07
相关资源
最近更新 更多