Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在uiwebview中调用https url?_Ios_Ssl_Uiwebview - Fatal编程技术网

Ios 如何在uiwebview中调用https url?

Ios 如何在uiwebview中调用https url?,ios,ssl,uiwebview,Ios,Ssl,Uiwebview,我有一个HTTPS url。它正在iPhone Safari中加载,但在UIWebView中不起作用 错误是: NSURLConnection/CFURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9813) 如何解决此问题?我已经在下面解释了如何在UIWebview中访问https url。它将帮助您清除此问题,因为它对我有效 调用http与https url相同 但是,如果您使用的是自签名证书,则有必要添加一些附加代码。因为默认情况下,iOS将拒

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

错误是:

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


如何解决此问题?

我已经在下面解释了如何在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-您的代码中WebVew在哪里?你能用webview解释一下代码吗?我们用Tianium把上面的代码放在哪里?你的问题是如何解决的?你用过@Durai Amuthan的答案吗?如果是,那么您是如何将数据加载到webview的?