Alamofire 2不允许与Instagram API进行SSL连接

时间:2015-10-03 23:39:02

标签: swift ssl swift2 alamofire

我正在使用Alamofire 1和Swift 1.2,以下请求正在运行:

let instagramUrl = "https://api.instagram.com/v1/media/search?omittedKey=omittedValue"
Alamofire.request(.GET, instagramUrl).responseJSON { (_, _, JSON) in ...

当我更新到Swift 2和Alamofire 2时,我收到了这个错误:

NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, NSUnderlyingError=0x7d0580f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, _kCFNetworkCFStreamSSLErrorOriginalValue=-9802, _kCFStreamErrorCodeKey=-9802, _kCFStreamErrorDomainKey=3, kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x7b7ef7d0>, kCFStreamPropertySSLPeerCertificates=<CFArray 0x7b6805f0 [0x1e64098]>{type = immutable, count = 2, values = (
0 : <cert(0x7b7eee80) s: *.instagram.com i: DigiCert High Assurance CA-3>
1 : <cert(0x7b7ef1f0) s: DigiCert High Assurance CA-3 i: DigiCert High Assurance EV Root CA>
)}}}, _kCFStreamErrorCodeKey=-9802, NSErrorFailingURLStringKey=https://api.instagram.com/v1/media/search?omittedKey=omittedValue, NSErrorPeerCertificateChainKey=<CFArray 0x7b6805f0 [0x1e64098]>{type = immutable, count = 2, values = (
0 : <cert(0x7b7eee80) s: *.instagram.com i: DigiCert High Assurance CA-3>
1 : <cert(0x7b7ef1f0) s: DigiCert High Assurance CA-3 i: DigiCert High Assurance EV Root CA>
)}, NSErrorClientCertificateStateKey=0, NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x7b7ef7d0>, NSErrorFailingURLKey=https://api.instagram.com/v1/media/search?omittedKey=omittedValue}

所以我试着用它:

let serverTrustPolicies: [String: ServerTrustPolicy] = [
    "api.instagram.com": .DisableEvaluation,
    "*.instagram.com": .DisableEvaluation
]

let manager = Manager(
    configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
    serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
manager.request(.GET, instagramUrl).responseJSON { (_, _, JSON) in

我收到了这个错误:

Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey=https://api.instagram.com/v1/media/search?omittedKey=omittedValue, NSErrorFailingURLStringKey=https://api.instagram.com/v1/media/search?omittedKey=omittedValue, NSLocalizedDescription=cancelled}

我尝试的最后一种方法是使用CustomServerTrustPolicyManager,但我不知道在func serverTrustPolicyForHost(host: String)上添加什么

class CustomServerTrustPolicyManager: ServerTrustPolicyManager {
    override func serverTrustPolicyForHost(host: String) -> ServerTrustPolicy? {
        var policy: ServerTrustPolicy?

        policy = ServerTrustPolicy.DisableEvaluation

        return policy
    }
}

并替换了

let manager = Manager(
    configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
    serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)

let manager = Manager(
    configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
    serverTrustPolicyManager: CustomServerTrustPolicyManager(policies: serverTrustPolicies)
)

但我得到了同样的错误:

Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey=https://api.instagram.com/v1/media/search?omittedKey=omittedValue, NSErrorFailingURLStringKey=https://api.instagram.com/v1/media/search?omittedKey=omittedValue, NSLocalizedDescription=cancelled}

我能做些什么才能让它发挥作用?最好是正确的方式。

感谢。

1 个答案:

答案 0 :(得分:0)

您的manager实例正在被释放,因为您没有维护对它的引用,或者您在查询字符串中传递的凭据无效。