Sorry for my dummy question but I'm new on Swift development, I have a project on Swift 1.1 which was working properly but after upgrading to Xcode to 7.3.1 (With Xcode 2.2.1) my project is built successfully but I'm getting an error while running it (on the let request line) with the following code:
// Send HTTP GET Request
let request = NSURLRequest(URL: NSURL(string: "http://11.22.33.44:8080/MySRV/login?email=\(emailField.text)&password=\(pwdField.text)")!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{
(response: NSURLResponse?, data: NSData?, error: NSError?)-> Void in
print("response \(response?.description)")
the error is:
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) fatal error: unexpectedly found nil while unwrapping an Optional value
I can imagine that my let request is nil but I don't know how to solve it.
Thanks for your help.
NSURLConnection
is now deprecated, so you should now useNSURLSession
(or in Swift 3,URLSession
). 2. You should not include a password in a URL. You should build aPOST
request and include the two parameters in the body of the request. I'm assuming your eventual server will employ HTTPS, but regardless it is recommended to not put sensitive information in the URL as some old proxy servers may capture that. 3. You should be percent escaping the email address and password. This will fail if they contained reserved characters (notably+
or&
). – Rob