Swift - Nsurl Error

What are the NSURLErrorDomain error code descriptions?

The NSURLErrorDomain error codes are listed here.

However, 400 is just the http status code (http://www.w3.org/Protocols/HTTP/HTRESP.html) being returned which means you've got something wrong with your request.

Swift iOS 9 NSURLErrorDomain Error -1004

After messing around with a ton of different keys and values to make it work, I finally have come up with this to make it function in iOS9.1:

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>mydomain.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSRequiresCertificateTransparency</key>
<false/>
</dict>
</dict>
</dict>

After I added the last one NSRequiresCertificateTransparency and set that to false it worked, so that's probably the most important one.

NSURL Error Code 1022

So, the problem had to do with ATS (App Transport Security), a new feature in iOS 9 that checks the authenticity of a URL before connecting to it. As I am not concerned about the secure-ness of the website I'm connecting to, I disabled ATS entirely. This can be done by adding the following code to the app's info.plist file:

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

Swift - NSURL error

The NSURL constructor you're calling has got this signature:

convenience init?(string URLString: String)

? means that the constructor may not return a value, hence it is considered as an optional.

Same goes for the NSData constructor:

init?(contentsOfURL url: NSURL)

A quick fix is:

let myProfilePictureURL = NSURL(string: "http://graph.facebook.com/bobdylan/picture")
let imageData = NSData(contentsOfURL: myProfilePictureURL!)
self.myImage.image = UIImage(data: imageData!)

The best solution is to check (unwrap) those optionals, even if you're sure that they contain a value!

You can find more infos on optionals here: link to official Apple documentation.

NSURLConnection finished with error - code -1002 when trying to encode URL in iOS

You are not creating your URL correctly. You are passing a path to the string argument. You need to use the URL(fileURLWithPath:) initializer.

let url = URL(fileURLWithPath: path)

Only use the URL(string:) initializer if the string you pass is a valid URL beginning with a URL scheme.



Related Topics



Leave a reply



Submit