Alamofire分析

Manager 生成RequestRequest
根据请求不同生成不同的delegate对象
SessionDelegate 利用subdelegates存储对应Request的对应的deleagte
同时实现一些通用的SessionDelegate通过self[task] 调用对应的delegate方法
之所以这么实现大概是为了是Request能处理文件上传,下载,同时请求数据。参考 http://www.cocoachina.com/ios/20151118/14240.html
另外信任无效证书代码如下

func acceptInvalidSSLCerts() {  
    let manager = Alamofire.Manager.sharedInstance  
    print("trying to accept invalid certs")  
      
    manager.delegate.sessionDidReceiveChallenge = { session, challenge in  
        var disposition: NSURLSessionAuthChallengeDisposition = .PerformDefaultHandling  
        var credential: NSURLCredential?  
          
        print("received challenge")  
          
        if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {  
            disposition = NSURLSessionAuthChallengeDisposition.UseCredential  
            credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)  
        } else {  
            if challenge.previousFailureCount > 0 {  
                disposition = .CancelAuthenticationChallenge  
            } else {  
                credential = manager.session.configuration.URLCredentialStorage?.defaultCredentialForProtectionSpace(challenge.protectionSpace)  
                  
                if credential != nil {  
                    disposition = .UseCredential  
                }  
            }  
        }  
          
        return (disposition, credential)  
    }  
}  
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容