gpt4 book ai didi

ios - 如何在uiwebview中调用https url?

转载 作者:技术小花猫 更新时间:2023-10-29 11:01:50 25 4
gpt4 key购买 nike

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

错误是:

NSURLConnection/CFURLConnection HTTP load failed (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

希望对你有帮助

关于ios - 如何在uiwebview中调用https url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20365774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com