1

I just had gone through the tutorial for setting up my first test iOS app in Xcode. All that went well. I have done a lot of web site development, I am very familiar with making data requests. I have an ubuntu server in the cloud that I want to use to return data to the app.

What is the best way to do this?

I tried setting up a basic http request, but I got an error saying that iOS by default doesn't allow non-secure connections. I then set up HTTPS on my server, and confirmed that I can get the data through the web browser, even though it warns me that the certificate isn't trusted. OK, so then from my iOS app and it gives me some errors about NSURLErrorFailingURLPeerTrustErrorKey and even one line returned saying: NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?. All my code is here right now:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, NSURLSessionDelegate {
    // MARK: Properties
    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var mealNameLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Handle the text field’s user input through delegate callbacks.
        nameTextField.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: UITextFieldDelegate
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        // Hide the keyboard.
        textField.resignFirstResponder()
        return true
    }

    func textFieldDidEndEditing(textField: UITextField) {
        mealNameLabel.text = nameTextField.text
    }

    // MARK: Actions
    @IBAction func setDefaultLabelText(sender: UIButton) {
        mealNameLabel.text = "Default Text"
        post_request()
    }

    func post_request(){
        let request = NSMutableURLRequest(URL: NSURL(string: "https://54.164.XXX.XX/post_script.php")!)
        request.HTTPMethod = "POST"
        let postString = "id=13&name=Jack"
        request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)

        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
            data, response, error in

            if error != nil {
                print("error=\(error)")
                return
            }

            print("response = \(response)")

            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("responseString = \(responseString)")
        }
        task.resume()
    }

}

So what is the best way to set up the data request? Is HTTPS the only option? Is it possible to make a secure connection to get data without buying a proper ssl certificate? I mean I connect to servers with ssh all the time and its free. What are the options for getting data from a server with iOS?

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
jeffery_the_wind
  • 17,048
  • 34
  • 98
  • 160
  • 2
    Check the following thread http://stackoverflow.com/questions/19507207/how-do-i-accept-a-self-signed-ssl-certificate-using-ios-7s-nsurlsession-and-its – casillas Nov 12 '15 at 15:36
  • So I have read a lot about people trying to all this "untrusted" certificate for dev purposes. That is OK for me now, but I can't quite figure out how to put the code together. From your link I have copied the code for swift `func URLSession(...`. how do I get this to actually handle the connection I am making in my code? – jeffery_the_wind Nov 12 '15 at 16:02

1 Answers1

1

This has been a common issue with iOS, you have to connect to a proper SSL certificate. There used to be hidden classes within iOS that would allow you to connect to non-standard sites which would be helpful during testing but these classes had to be removed prior to release or Apple would reject.

The final answer though is that if you must use SSL (https) the site must have a valid/proper SSL certificate.

BTW, if iOS doesn't allow it by default, it would appear that you can change your app (permissions?) to allow it anyway. I would suggest trying that as secured communications is not a necessity at all times.

KevinDTimm
  • 14,226
  • 3
  • 42
  • 60
  • I have also read a lot about people overriding this authentication. There are a lot of SO threads about it, but I can't quite figure out how to fit it into my code. Still a little new to iOS and swift. – jeffery_the_wind Nov 12 '15 at 16:03