使用WeexSDK,网络请求信任证书的问题
使用0.18.0版本的weexSDK,并且是手動導入的SDK。
在項目中創建一個SDResourceRequest(名字隨意)類繼承WXResourceRequestHandlerDefaultImpl,遵循
WXResourceRequestHandler,NSURLSessionDelegate協議,如下圖:
在SDResourceRequest.m中實現- (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;//判斷服務器返回的證書是否是服務器信任的if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];/*disposition:如何處理證書NSURLSessionAuthChallengePerformDefaultHandling:默認方式處理NSURLSessionAuthChallengeUseCredential:使用指定的證書NSURLSessionAuthChallengeCancelAuthenticationChallenge:取消請求*/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導入WeexSDK應該沒有問題,但是手動導入的WeexSDK可能沒有WXResourceRequestHandlerDefaultImpl.h,可以在git上下載weexSDK源碼把WXResourceRequestHandlerDefaultImpl.h放在外面,重新打包后在導入項目,如圖:
?
?
參考:
AFNetworking中的處理方式的參考鏈接找不到了
https://www.jianshu.com/p/bcb19fe43909
https://www.jianshu.com/p/3cc2ec005761
?
轉載于:https://www.cnblogs.com/lulushen/p/9640150.html
總結
以上是生活随笔為你收集整理的使用WeexSDK,网络请求信任证书的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows下如何访问linux下的o
- 下一篇: 史上最简单MySQL教程详解(进阶篇)之