2014-10-22 4 views
5

내가 인증서를 허용하는 데 사용하는 코드입니다 :WKWebView HTTPS 인증서 유효 여기

@interface NSURLRequest(DummyInterface) 
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host; 
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host; 
@end 
@implementation NSURLRequest(DummyInterface) 
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host 
{ 
    return [host isEqualToString:@"mysite.com"]; 
} 
@end 

을 그리고 난 그런 내 WKWebView 초기화 :

NSURL *urlReq = [NSURL URLWithString:@"mysite.com"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:urlReq]; 
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[urlReq host]]; 

WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init]; 
mainWebView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration]; 
[mainWebView setNavigationDelegate:self]; 
[mainWebView loadRequest:request]; 

그것은 HTTP 웹 사이트에 대한 잘 작동하지만 내가 가진이 오류 : https :

이 서버 인증서는 유효하지 않습니다. 에 "mysite.com"인 척하는 서버를 연결했을 수 있습니다. 은 기밀 정보를 위험에 빠뜨릴 수 있습니다.

UIWebView를 사용하고 "canAuthenticateAgainstProtectionSpace"기능을 구현할 때 작동하지만, 이제는 내가해야 할 일을 이해하지 못합니다.

내가 누락되었거나 WKWebView가 HTTPS를 처리 할 수 ​​없습니까?

+0

이 문제가 해결 되었습니까? – David

+0

솔직히 말해서 클래식 UIWebView로 다시 전환했는데 완벽하게 작동합니다. 이후 새로운 WKWebView를 시도하지 않았습니다 ... 죄송합니다. – Pull

+0

누구나 해결책이 있습니까? 나는 WKWebView에서 같은 문제에 당장 붙어있다. allowsAnyHTTPSCertificateForHost Private API를 사용하여 UIWebview에서 잘 작동하지만 WKWebView에서는 작동하지 않습니다. – harshit2811

답변

5

, 날

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler { 
    NSLog(@"Allowing all"); 
    SecTrustRef serverTrust = challenge.protectionSpace.serverTrust; 
    CFDataRef exceptions = SecTrustCopyExceptions (serverTrust); 
    SecTrustSetExceptions (serverTrust, exceptions); 
    CFRelease (exceptions); 
    completionHandler (NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:serverTrust]); 
} 

위해 일을 추가하는 것을 잊지 마세요 Info.plist로 이동

<key>NSAppTransportSecurity</key> 
<dict> 
    <key>NSAllowsArbitraryLoads</key> 
    <true/> 
</dict>