Tuesday, October 13, 2015

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.[Solved]!!!

You will get this error when upgrade the source from Swift 1.2 to Swift 2.0 and using some website URL in your app.

We can easily solve this issue. You can try the below solution to solve this issue.

Issue:

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.

Error: The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.

Solution:

Edit your info.plist file and add the new key called NSAppTransportSecurity with type Dictionary. Then create new key called NSAllowsArbitraryLoads with type Boolean and set that value to YES.
NSAllowsArbitraryLoads value is the Dictionary item of NSAppTransportSecurity.

Refer the below snapshot.


NSURLConnection Syntax for Swift 2.0

NSURLConnection Syntax for Swift 1.2

var jsonSource: NSData =  NSURLConnection.sendSynchronousRequest(URLrequest, returningResponse: response, error:nil)!



NSURLConnection Syntax for Swift 2.0

let jsonSource: NSData =  try! NSURLConnection.sendSynchronousRequest(URLrequest, returningResponse: response)

NSJSONSerialization Syntax for Swift 2.0

NSJSONSerialization Syntax for Swift 1.2

if let jsonArray: NSArray = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSArray?


NSJSONSerialization Syntax for Swift 2.0


if let jsonArray: NSArray = (try? NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSArray?

NSAttributedString Syntax for Swift 2.0

NSAttributedString Syntax for Swift 1.2 

let attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil)!


NSAttributedString Syntax for Swift 2.0 


let attributedString = try! NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)