Search code examples
iosjsonxcodeswiftapp-transport-security

Can't open HTTP links in iOS due to security issue


The solution to the problem but still getting the error

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.

I have tried the solution on the picture but still getting the error

   import UIKit

   class ViewController: UIViewController {


   lazy var data = NSMutableData()


    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.



    let url = NSURL(string: "http://android.goidx.com/search")
    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
        if error != nil {
            print(error)
            } else {
            do {
                let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as!  NSDictionary
                // print(jsonResult)

                print(jsonResult[0])
            } catch {
                print("my error")
            }
        }

    })

    task.resume()





}

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




 }

Solution

  • Your plist file should look like this: enter image description here And note that your code crashes the app, because you are casting to a dictionary where it should be an array:

     override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
    
    
    
            let url = NSURL(string: "http://android.goidx.com/search")
            let session = NSURLSession.sharedSession()
            let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
                if error != nil {
                    print(error)
                } else {
                    do {
                        let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as!  NSArray
                        // print(jsonResult)
    
                        print(jsonResult[0])
                    } catch {
                        print("my error")
                    }
                }
    
            })
    
            task.resume()
    
    
    
    
    
        }
    

    All should then work - it does for me.