8

Unfortunately this morning my XCode updated to version 7 and the iOS app I was developing with http now wants https. So, following many tutorials, I configured my MAMP server in order to use https/ssl creating a dummy certificate. Now in my iOS app URLs are like the following:

static var webServerLoginURL = "https://localhost:443/excogitoweb/mobile/loginM.php"
static var webServerGetUserTasks = "https://localhost:443/excogitoweb/mobile/handleTasks.php"
static var webServerGetUsers = "https://localhost:443/excogitoweb/mobile/handleUsers.php"
static var webServerGetProjects = "https://localhost:443/excogitoweb/mobile/handleProjects.php"

and they work fine if I try to access them in my browser. I was used to access the database and php files with NSURLSession.sharedSession().dataTaskWithRequest() which now raises the error in title. For example, here's the line where error is raised:

if let responseJSON: [[String: String]] = (try? NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())) as? [[String: String]] {
...
}

and this is the complete error message:

2015-09-21 16:41:48.354 ExcogitoWeb[75200:476213] CFNetwork SSLHandshake failed (-9824)
2015-09-21 16:41:48.355 ExcogitoWeb[75200:476213] NSURLSession/NSURLConnection   HTTP load failed (kCFStreamErrorDomainSSL, -9824)
fatal error: unexpectedly found nil while unwrapping an Optional value

I would like to know how to fix this. I've read some useful answers here but there are many things I still don't understand and if anyone would help/explain me I'd be very grateful.

SagittariusA
  • 5,289
  • 15
  • 73
  • 127

3 Answers3

12

Add this to your app's Info.plist

<key>NSAppTransportSecurity</key>  
    <dict>  
    <key>NSAllowsArbitraryLoads</key>  
    <true/>  
    </dict>
Carmen
  • 6,177
  • 1
  • 35
  • 40
mychar
  • 1,031
  • 1
  • 11
  • 20
3
fatal error: unexpectedly found nil while unwrapping an Optional value

usually means that you're doing something not so good, and by looking at your if

if let responseJSON: [[String: String]] = (try? NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())) as? [[String: String]] {

I can see that there's a data!, but that data object is nil. You really should unwrap the optionals before using them, especially when working with remote data.

Then you have a network error, that's probably related to the ATS Apple added in iOS 9.

See another answer on how to temporarily disable ATS. https://stackoverflow.com/a/30748166/421755

edit: I see now that you added ssl to your localhost, that's good. However it's not enough for ATS to work, since it needs TLS 1.2 and not self-signed certificates.

Community
  • 1
  • 1
Simon
  • 1,076
  • 7
  • 13
  • Yes, I guess the problem is that data is nil because of that error network, I'll try to disable it as you said...all I have understood is that I should upgrade MAMP ssl/tls to 1.2...is it possible now? Or shall we wait? – SagittariusA Sep 21 '15 at 15:03
  • 1
    I just edited the answer at the same time you commented. I think it's way easier to just disable `ATS` when using a local server. Make sure you enable it later on when you ship the app! – Simon Sep 21 '15 at 15:06
  • I've just read that TLS 1.2 will be available with OSX El Capitan...I really hope so. I'm developing this iOS app for an exam and I wouldn't like to look like a inexperienced guy to my teacher...thank you for your healp – SagittariusA Sep 21 '15 at 15:09
  • sorry for the disturb..would you please tell me how to edit plist files? Because I don't see it with xml syntas... – SagittariusA Sep 21 '15 at 15:12
  • sure, just right click on your plist-file in xcode, and select `Open As` `Source Code`. http://imgur.com/eq94tju – Simon Sep 21 '15 at 15:15
  • I thought I had made it work but now I get this: "NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)"... – SagittariusA Sep 21 '15 at 15:25
  • This is what I've added: – SagittariusA Sep 21 '15 at 15:34
  • NSAppTransportSecurity NSExceptionDomains localhost NSIncludesSubdomains NSTemporaryExceptionAllowsInsecureHTTPLoads NSTemporaryExceptionMinimumTLSVersion TLSv1.1 – SagittariusA Sep 21 '15 at 15:34
1

Really old question, but I thought I would respond. This usually happens when you try to unwrap an http object and it comes back nil.

Make sure that you unwrap your url and give it a default value.

example :

let jsonURL = "your https link to your json"
guard let url = URL(string: jsonURL) else { return } // dont force unwrap

hope this helps!

BitBeast
  • 33
  • 7