-1

Please have a look at the following code:

override func viewDidLoad() {
  super.viewDidLoad()

  let url2 = NSURL (string: "http://leinelab.net/api/spaceapi.json")
  let requestObj = NSURLRequest(URL: url2!);
  let response: AutoreleasingUnsafeMutablePointer<NSURLResponse?> = nil;
  let responseData = (try? NSURLConnection.sendSynchronousRequest(requestObj, returningResponse: response)) as NSData?

  let datastring = NSString(data: responseData!, encoding: NSUTF8StringEncoding)  
  let datastring2 = String(datastring!)

  let text = ":true}"
  if datastring2.rangeOfString(text) != nil
  .
  .
  .

Somewhere here I have to work with NSURLConnection to access a website with self signed certificates but I don't know how. Code worked under Xcode 6, after upgrading to Xcode 7 it doesn't.

Edit:

This is the error message:

2016-01-22 22:45:56.678 LL-Test[3968:147205] App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file. fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
halfer
  • 19,824
  • 17
  • 99
  • 186
Tapnerd
  • 1
  • 1
  • Does the app crash or does it not compile? And do you get any error-message? BTW: `NSURLConnection` is deprecated in iOS 9. You should use `NSURLSession` – FelixSFD Jan 22 '16 at 21:44
  • Please can you edit your question so all your code is within a code block; it makes the question much easier to read. Also, add to the question some more detail about what does not work; is it a crash? is there an error message? does the code refuse to compile? Details please. – Robotic Cat Jan 22 '16 at 21:45
  • 1
    Build works, no crash. The app simply does not start. This is the error message: 2016-01-22 22:45:56.678 LL-Test[3968:147205] App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file. fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) – Tapnerd Jan 22 '16 at 21:47
  • Here is the complete code: – Tapnerd Jan 22 '16 at 21:51
  • Sorry, complete code is too long. – Tapnerd Jan 22 '16 at 22:04

1 Answers1

2

You have to use HTTPS since iOS 9. If you want to use HTTP, you have to configure exceptions in your info.plist. Because of this behaviour, datastring and datastring2 are nil, which causes the crash.

To configure the "App Transport Security"-exceptions, read this post in the Apple Developer Forum: https://forums.developer.apple.com/thread/3544

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
  • HTTPS does not work, either. Reason seems to be the self signed certificate. But I think I found something in the forum post. Thank you very much! – Tapnerd Jan 22 '16 at 22:17