zoukankan      html  css  js  c++  java
  • 使用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

  • 相关阅读:
    每天一点正能量
    嵌入式开发-写在这里作为参照
    产品设计与开发 之 开发流程和组织架构
    产品设计与开发 之 开头语
    机器视觉基础
    项目与运营
    3.Linux如何管理分区
    2.Linux系统之硬盘与分区基础知识
    1.Linux入门介绍
    shell笔记
  • 原文地址:https://www.cnblogs.com/lulushen/p/9640150.html
Copyright © 2011-2022 走看看