使用WeexSDK,网络请求信任证书的问题
使用0.18.0版本的weexSDK,并且是手動(dòng)導(dǎo)入的SDK。
在項(xiàng)目中創(chuàng)建一個(gè)SDResourceRequest(名字隨意)類繼承WXResourceRequestHandlerDefaultImpl,遵循
WXResourceRequestHandler,NSURLSessionDelegate協(xié)議,如下圖:
在SDResourceRequest.m中實(shí)現(xiàn)- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler方法,代碼如下:
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler {//AFNetworking中的處理方式NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;__block NSURLCredential *credential = nil;//判斷服務(wù)器返回的證書是否是服務(wù)器信任的if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];/*disposition:如何處理證書NSURLSessionAuthChallengePerformDefaultHandling:默認(rèn)方式處理NSURLSessionAuthChallengeUseCredential:使用指定的證書NSURLSessionAuthChallengeCancelAuthenticationChallenge:取消請(qǐng)求*/if (credential) {disposition = NSURLSessionAuthChallengeUseCredential;} else {disposition = NSURLSessionAuthChallengePerformDefaultHandling;}} else {disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;}//安裝證書if (completionHandler) {completionHandler(disposition, credential);}}
?
使用下面的方法無效,不知道為什么
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler{/*方法一 信任所有證書*/if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];if(completionHandler)completionHandler(NSURLSessionAuthChallengeUseCredential,credential);} }?
使用cocoaPods導(dǎo)入WeexSDK應(yīng)該沒有問題,但是手動(dòng)導(dǎo)入的WeexSDK可能沒有WXResourceRequestHandlerDefaultImpl.h,可以在git上下載weexSDK源碼把WXResourceRequestHandlerDefaultImpl.h放在外面,重新打包后在導(dǎo)入項(xiàng)目,如圖:
?
?
參考:
AFNetworking中的處理方式的參考鏈接找不到了
https://www.jianshu.com/p/bcb19fe43909
https://www.jianshu.com/p/3cc2ec005761
?
轉(zhuǎn)載于:https://www.cnblogs.com/lulushen/p/9640150.html
總結(jié)
以上是生活随笔為你收集整理的使用WeexSDK,网络请求信任证书的问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows下如何访问linux下的o
- 下一篇: selective gaussian b