如何在uiwebview中调用https url? [英] How to call https url in uiwebview?

查看:96
本文介绍了如何在uiwebview中调用https url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTTPS网址。它在iPhone Safari中加载,但在 UIWebView 中无效。

I have an HTTPS url. It is loading in iPhone Safari, but didn't work in UIWebView.

错误是:


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

NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)

如何解决此问题?

推荐答案

我在下面已经解释了如何在UIWebview中访问https网址它会对你有所帮助清除问题因为它对我有用。

I have explained below how to access https url in UIWebview it will help you clear the problem because it works for me.

调用http与https网址相同。

Calling http is same as https url.

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

If, however, you're using a self-signed certificate, it's necessary to add some additional code.Because by default iOS will reject all untrusted https connections.

限制不受信任的连接是非常好的默认值行为和任何禁用是非常危险的。所以我们将显示警报,因为我们将绕过默认行为。

Restricting untrusted connections is very good default behaviour and any disabling of this is highly risky.So we will show an alert as we're going to bypass the default behaviour.

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

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

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

These two above methods allows us to provide our own authentication mechanism for trusted connections

#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

希望这会有所帮助

这篇关于如何在uiwebview中调用https url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆