My Blog

Thoughts on iOS developement

Server Trust Evaluation Has Failed

While working iOS app I faced an issue when app was making a connection to API endpoint. After investigation I found that it was happening because of server trust evaluation failure.

1
2
Error I was getting,
Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “example.com” which could put your confidential information at risk." UserInfo=0x14a730 {NSErrorFailingURLStringKey=https://example.com/, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSErrorFailingURLKey=https://example.com/, NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “example.com” which could put your confidential information at risk., NSUnderlyingError=0x14a6c0 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “example.com” which could put your confidential information at risk.", NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x14ec00>}

Apple have really good technical note on this matter.

NSURLSession

Here is how I fixed it using NSURLSession delegates,

1
2
3
4
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
    completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}

NSUserDefaults Data and iOS8 Simulator

This is a new behaviour I’ve noticed in iOS 8 simulator that deleting applications from simulator no longer deleting data stored in NSUserDefaults. Not quite sure it’s a bug or a feature.

You can reset content and setting in simulator but if you don’t want to do it then you can delete the application from simulator and then delete the plist file from,

1
Macintosh HD ▸ Users ▸ username ▸ Library ▸ Developer ▸ CoreSimulator ▸ Devices ▸ SimulatorCrypticNumber ▸ data ▸ Library ▸ Preferences ▸ YourApplicationBundleID.plist

In the Devices folder you’ll see folder for each simulator, check deviceType in device.plist to check which simulator folder it is.

If you are like me who don’t want to remember the location then you can create a shortcut in your sidebar for that location.