首页 文章

由于安全问题,无法在iOS中打开HTTP链接

提问于
浏览
0

The solution to the problem but still getting the error

App Transport Security已阻止明文HTTP(http://)资源加载,因为它不安全 . 可以通过应用程序的Info.plist文件配置临时例外 .

我已经在图片上尝试了解决方案,但仍然收到错误

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.
  }




 }

1 回答

  • 1

    您的plist文件应如下所示:
    enter image description here
    并注意您的代码崩溃了应用程序,因为您正在转换为应该是数组的字典:

    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()
    
    
    
    
    
        }
    

    一切都应该工作 - 它对我有用 .

相关问题