使用0.18.0版本的weexSDK,并且是手动导入的SDK。

在项目中创建一个SDResourceRequest(名字随意)类继承WXResourceRequestHandlerDefaultImpl,遵循

WXResourceRequestHandler,NSURLSessionDelegate协议,如下图:

使用WeexSDK,网络请求信任证书的问题

在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放在外面,重新打包后在导入项目,如图:

使用WeexSDK,网络请求信任证书的问题

 

 

参考:

AFNetworking中的处理方式的参考链接找不到了

https://www.jianshu.com/p/bcb19fe43909

https://www.jianshu.com/p/3cc2ec005761

 

相关文章:

  • 2021-10-16
  • 2022-12-23
  • 2022-01-08
  • 2021-09-13
  • 2021-10-09
  • 2021-06-25
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-24
  • 2022-12-23
  • 2021-09-11
  • 2022-12-23
  • 2022-01-04
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案