SSLHandshake在Alamofire 3.1.2,XCode 7.1,iOS 9

时间:2015-11-13 06:08:05

标签: swift ssl ios9 alamofire

我对Swift项目进行了以下升级

  1. iOS 9,XCode 7.1,El Captain
  2. Alamofire 1.3 - > 3.1.2
  3. 连接到自签名(测试)Web服务时出现以下错误。

    错误消息

      

    2015-11-12 21:46:13.045 [72695:5258083] CFNetwork SSLHandshake失败   (-9801)2015-11-12 21:46:13.046 [72695:5258083]   NSURLSession / NSURLConnection HTTP加载失败   (kCFStreamErrorDomainSSL,-9801)2015-11-12 21:46:13.051 [错误]   [DeviceInfo.swift:277] generateUserToken>处理时出错   generateUserToken():可选(错误域= NSURLErrorDomain   Code = -1200“发生了SSL错误,并且安全连接到   服务器无法生成。“UserInfo = {_ kCFStreamErrorCodeKey = -9801,   NSLocalizedRecoverySuggestion =您想要连接到服务器吗?   无论如何?,NSUnderlyingError = 0x7ffa99a0b8c0 {错误   Domain = kCFErrorDomainCFNetwork Code = -1200“(null)”   的UserInfo = {_ kCFStreamPropertySSLClientCertificateState = 0,   _kCFNetworkCFStreamSSLErrorOriginalValue = -9801,_kCFStreamErrorDomainKey = 3,_kCFStreamErrorCodeKey = -9801}},NSLocalizedDescription =发生了SSL错误并且安全   无法建立与服务器的连接。

    我已根据https://github.com/Alamofire/Alamofire#security

    实施了以下代码来禁用SSL验证
    class CustomServerTrustPolicyManager: ServerTrustPolicyManager {
        override func serverTrustPolicyForHost(host: String) -> ServerTrustPolicy? {
            var policy: ServerTrustPolicy?
            policy = ServerTrustPolicy.DisableEvaluation
            return policy
        }
    }
    
    public class NetworkManager {
    
        static let sharedInstace = NetworkManager()
    
        let defaultManager : Manager = {
    
            let serverTrustPolicies: [String: ServerTrustPolicy] = [
                "myservice.com": .DisableEvaluation
            ]
    
            let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
            configuration.HTTPAdditionalHeaders = Alamofire.Manager.defaultHTTPHeaders
    
            return Alamofire.Manager(
                configuration: configuration,
                serverTrustPolicyManager: CustomServerTrustPolicyManager(policies: serverTrustPolicies)
            )
    
        }()
    
    }
    

    另外,我在info.plist中添加了以下条目,但没有运气。

    <dict>
        <key>NSAppTransportSecurity</key>
        <dict>
            <key>NSAllowsArbitraryLoads</key>
            <true/>
            <key>NSExceptionDomains</key>
            <dict>
                <key>myservice.com</key>
                <dict>
                    <key>NSIncludesSubdomains</key>
                    <true/>
                    <key>NSExceptionAllowsInsecureHTTPLoads</key>
                    <true/>
                    <key>NSExceptionRequiresForwardSecrecy</key>
                    <false/>
                    <key>NSExceptionMinimumTLSVersion</key>
                    <string>TLSv1.1</string>
                    <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
                    <true/>
                    <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
                    <false/>
                    <key>NSThirdPartyExceptionMinimumTLSVersion</key>
                    <string>TLSv1.1</string>
                    <key>NSRequiresCertificateTransparency</key>
                    <true/>
                </dict>
            </dict>
        </dict>
    </dict>
    

    是否有人使用Alamofire&amp; amp;成功连接自签名服务iOS9?请指教。

0 个答案:

没有答案