iOS HTTPS 自檢證書請(qǐng)求服務(wù)器和WKWebView

1、使用AFN3.0 HTTPS網(wǎng)絡(luò)請(qǐng)求

    +(AFSecurityPolicy *)customSecurityPolicy{     //簡單封裝一下

    //2 設(shè)置證書模式
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"mnchip" ofType:@"cer"];

    NSData *cerDat = [NSData dataWithContentsOfFile:cerPath];

    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];

    //允許自檢證書
    securityPolicy.allowInvalidCertificates = YES;

    //域名與服務(wù)器一致
    securityPolicy.validatesDomainName = YES;

    securityPolicy.pinnedCertificates = [[NSSet alloc] initWithObjects:cerDat, nil];

    return securityPolicy;

    }

在有網(wǎng)絡(luò)請(qǐng)求的地方:

******

    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    //添加證書
    [session setSecurityPolicy:[OperationHelper customSecurityPolicy]];

******

這樣就可以了

2、關(guān)于WKWebView , 我是將Allow Arbitrary Loads in Web Content 置為 yes,然后正常使用,注意一點(diǎn),要實(shí)現(xiàn)這個(gè)代理方法


    - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{

    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {

    NSURLCredential *card = [[NSURLCredential alloc]initWithTrust:challenge.protectionSpace.serverTrust];

    completionHandler(NSURLSessionAuthChallengeUseCredential,card);

    }}

@implementation NSURLRequest(DataController)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
    return YES;
}
@end

關(guān)于WKWebView就可以正常顯示了

3、UIWebView 訪問https 繞過證書驗(yàn)證的方法

在AppDelegate.m里面添加下面這些代碼

@implementation NSURLRequest(DataController)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
    return YES;
}
@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容