I made the following updates to my Swift project
- iOS 9, Xcode 7.1, El Captain
- Alamofire 1.3 → 3.1.2
When connecting to a self-signed (test) web service, the following error appears.
Error message
2015-11-12 21: 46: 13.045 [72695: 5258083] CFNetwork SSLHandshake error (-9801) 2015-11-12 21: 46: 13.046 [72695: 5258083] NSURLSession / NSURLConnection loading error (kCFStreamErrorDomainSSL, -9801) 2015- 11-12 21: 46: 13.051 [Error] [DeviceInfo.swift: 277] generateUserToken> Error while processing generateUserToken (): Optional (Error Domain = NSURLErrorDomain Code = -1200 "An SSL error occurred and a secure connection to the server could not be created ". UserInfo = {_ kCFStreamErrorCodeKey = -9801, NSLocalizedRecoverySuggestion = Do you want to connect to the server? In any case ?, NSUnderlyingError = 0x7ffa99a0b8c0 {Error Domain = kCFErrorDomainCStNFrCtrcrtrctrctrclcrtrctrcrc_creamertreclient_function_function_function_file_create_function_file_create_file_function_file_function_function_file_function_file_function_function_string_function_function_function_function_function_function_function_function_function_function_function_function_function_function_function_function_function_function_function_function_function_file = -9801, _kCFStreamErrorDomainKey = 3, _kCF StreamErrorCodeKey = -9801}}, NSLocalizedDescription = An SSL error has occurred and the connection to the server cannot be secure.
I applied the code below to disable SSL verification at https://github.com/Alamofire/Alamofire#security
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) ) }() }
Also, I added the entry below to info.plist, but no luck.
<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>
Has anyone successfully connected self-signed services using Alamofire and iOS9? Please advice.
source share