Alamofire分析

Manager 生成Request
Request根据请求不同生成不同的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)
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值