php - Swift:NSURL 正确地从 Playground 中的 URL 返回 HTML,但在应用程序中使用时会使模拟器崩溃

标签 php xcode swift nsurl

我正在使用 NSURL 获取网站的 HTML,该网站在 Playground 中运行良好,但当我在应用程序中使用它时,模拟器会崩溃。 Playground 代码:

import Foundation
import XCPlayground

// Let asynchronous code run
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

let myUrl = NSURL(string: "http://www.google.com")
let request = NSMutableURLRequest(URL: myUrl!)
request.HTTPMethod = "POST"
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
  data, response, error in
  let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
  if error != nil {
    print("Error: \(error)")
  }
  print("Response: \(response)")
  print("Response String: \(responseString)")
}
task.resume()

应用程序代码:

import UIKit
import Foundation

class ViewController: UIViewController {

    @IBOutlet weak var testLabel: UILabel!

    @IBAction func testButton(sender: UIButton) {
        let myUrl = NSURL(string: "http://www.google.com")
        let request = NSMutableURLRequest(URL: myUrl!)
        request.HTTPMethod = "POST"
        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
            data, response, error in
            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            self.testLabel.text = "\(responseString)"
            if error != nil {
                print("Error: \(error)")
            }
        }
        task.resume()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

崩溃时出错:
2015-10-27 20:14:54.105 testProject[51314:431730] 应用程序传输安全已阻止明文 HTTP (http://) 资源加载,因为它不安全。可以通过应用程序的 Info.plist 文件配置临时异常(exception)。 fatal error :在解包可选值时意外发现 nil (LLDB)

谢谢,
尼克

最佳答案

我使用了以下代码:

class ViewController: UIViewController {

    @IBOutlet weak var testLabel: UILabel!

    @IBAction func testButton(sender: UIButton) {
        let myUrl = NSURL(string: "http://www.google.com")
        let request = NSMutableURLRequest(URL: myUrl!)
        request.HTTPMethod = "POST"
        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
            data, response, error in
            print("hey \(data)")
            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            self.testLabel.text = "\(responseString)"
            if error != nil {
                print("Error: \(error)")
            }
        }
        task.resume()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

在我的控制台中打印以下内容:

2015-10-28 02:54:49.284 trial[58972:2551861] 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.
hey nil
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) 

如您所见,data 为零。这可能与由于应用程序传输安全性而阻止 http 的事实有关。

关于php - Swift:NSURL 正确地从 Playground 中的 URL 返回 HTML,但在应用程序中使用时会使模拟器崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33380776/

相关文章:

javascript - 空 php session (if else 语句)

php - 我如何将这段文字和按钮与网页布局相匹配?

json - 如何在 swift xcode 中为多个 json 表创建结构

swift - 多个 Sprite 节点,仅出现一个

iphone - Xcode 组织者 : can not use iPhone (dyld_shared_cache_extract_dylibs failed)

ios - 从 Swift 的 TimeInterval 中删除时间组件?

javascript - 通过ajax对Mysql表进行排序

php - Laravel 驱动未找到,数据库连接错误?

swift - Swift 中 UIPickerView 顶部的搜索栏

ios - swift 3 : Is there a way to cast an object to a class and protocol at the same time?