简体   繁体   中英

Alamofire HTTPS request failing

I want to use Alamofire in my swift application, to get the data from server. But when I am sending request through Alamofire its always throwing an error. But if I'm using NSUrlSession with default configuration it's working.

I tried this solution but still its not working

Code which I am using

let headers = [
        "Authorization": validationHeader
    ]

    let completeUrl = kReserveBaseURL + strMyResURL
    Alamofire.request(.GET, completeUrl, headers: headers)
        .response { request, response, data, error in
            debugPrint(error)
            debugPrint(response)
    }

Below are the issues which I'm getting

    Some : Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “abc.xyz.com” which could put your confidential information at risk." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x7ff0c22052d0>, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9813, NSErrorPeerCertificateChainKey=<CFArray 0x7ff0c044b840 [0x10996ba40]>{type = immutable, count = 2, values = (
    0 : <cert(0x7ff0c22044c0) s: abc.xyz.com i: Verizon Public SureServer EV SSL CA G14-SHA2>
    1 : <cert(0x7ff0c2204770) s: Verizon Public SureServer EV SSL CA G14-SHA2 i: Cybertrust Global Root>
)}, NSUnderlyingError=0x7ff0c2302070 {Error Domain=kCFErrorDomainCFNetwork Code=-1202 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x7ff0c22052d0>, _kCFNetworkCFStreamSSLErrorOriginalValue=-9813, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9813, kCFStreamPropertySSLPeerCertificates=<CFArray 0x7ff0c044b840 [0x10996ba40]>{type = immutable, count = 2, values = (
    0 : <cert(0x7ff0c22044c0) s: abc.xyz.com i: Verizon Public SureServer EV SSL CA G14-SHA2>
    1 : <cert(0x7ff0c2204770) s: Verizon Public SureServer EV SSL CA G14-SHA2 i: Cybertrust Global Root>
)}}}, NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “abc.xyz.com” which could put your confidential information at risk., NSErrorFailingURLKey=https://abc.xyz.com/Api/CheckIn/GetData?userId=0&BookingDate=2016-04-16, NSErrorFailingURLStringKey=https://abc.xyz.com/Api/CheckIn/GetData?userId=0&BookingDate=2016-04-16, NSErrorClientCertificateStateKey=0}

It looks like you are getting this error due to iOS security restricitons (Untruster server certificate on the SSL level). I would recommend to troubleshoot this by adding security exception into you app's info.plist:

<key>NSAppTransportSecurity</key>
<dict>
    <!--Include to allow all connections (DANGER)-->
    <key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

Source link: https://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/

After adding this you should not observe the error any more.

If it helps, you can figure out depper using this:

https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html?hl=sw

Especialy relevant section to you is: "Using the nscurl tool to diagnose ATS Connection Issues"

Its because of the security restrictions. In the info.plist add these two comments, 看到这张图片

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM