let manager: Alamofire.Manager = { let manager = Alamofire.Manager.sharedInstance manager.delegate.sessionDidReceiveChallenge = { session, challenge in var disposition: NSURLSessionAuthChallengeDisposition = .PerformDefaultHandling var credential: NSURLCredential? 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) } return manager }()二、 UIWebView 配置https(因为我的工程全是swift写的,贴上swift代码,oc自己转吧!)
主要实现NSURLConnectionDelegate两个代理方法:func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool { return (protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) } func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge) { if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) { challenge.sender!.useCredential(NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!), forAuthenticationChallenge: challenge) challenge.sender!.continueWithoutCredentialForAuthenticationChallenge(challenge) } }这样就搞定了 ,坑了我两天啊,swift的更是不好找!本人菜鸟,哪不对欢迎提出
新闻热点
疑难解答