Ho appena aggiornato il mio codice a Swift 2.0 per funzionare con Xcode 7. La mia app esegue l'autenticazione NSURLAuthenticationMethodServerTrust
e NSURLAuthenticationMethodClientCertificate
.Autenticazione server in Swift 2.0 e XCode 7 interrotta
Il problema è NSURLAuthenticationMethodServerTrust
l'autenticazione ha smesso di funzionare sul mio simulatore, ma funziona ancora sul mio dispositivo di test con iOS 8.3. Oltre al mio vecchio progetto, che non è Swift 2.0, funziona ancora.
Errore: NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
errore recuperato dal NSURLSession:
Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo=0x7fcf75053070 {NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x7fcf73700d00>, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorCodeKey=-9802, NSUnderlyingError=0x7fcf735284b0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1200.)", NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSErrorFailingURLKey=https://mywebapi/dosomething, NSErrorFailingURLStringKey=https://mywebapi/dosomething, _kCFStreamErrorDomainKey=3} [GetOneTimeTokenController.swift:76]
sto ancora il targeting iOS 8.0 per la distribuzione.
Questo è come gestire la sfida di autenticazione (utilizzando un certificato firmato di auto):
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodClientCertificate {
let urlCredential:NSURLCredential = NSURLCredential(
identity: identityAndTrust.identityRef,
certificates: identityAndTrust.certArray as [AnyObject],
persistence: NSURLCredentialPersistence.ForSession);
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, urlCredential);
} else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(trust: challenge.protectionSpace.serverTrust!));
} else {
challenge.sender?.continueWithoutCredentialForAuthenticationChallenge(challenge)
Logger.sharedInstance.logMessage("Unexpected Authentication Challange", .Error);
}
Grazie. Solo un (probabilmente ovvio commento - per newb relativi come me) - metti questo in fondo al file, facendo attenzione ad avere ancora le ultime due righe, e dopo di esso. I miei primi tentativi di posizionarlo vicino alla parte superiore hanno influenzato la formattazione XML (riceverai un errore "Info.plist non nel formato corretto" se lo inserisci in modo errato). – user3741598