为什么我不能从此https请求中获得价值?

时间:2019-01-26 02:20:36

标签: swift http https request

有人知道这可能是什么问题吗?

每次我在模拟器中测试时,请求都只会返回值“请输入名称”。

就像根本没有发生https请求一样。

import UIKit

class AgeLookUpViewController: UIViewController {
    @IBOutlet weak var celebrityTextField: UITextField!
    @IBOutlet weak var resultLabel: UILabel!

    @IBAction func getAge(_ sender: Any) {
        if let url = URL(string: "https://en.wikipedia.org/wiki/" + celebrityTextField.text!.replacingOccurrences(of: " ", with: "_")) {
            let request = NSMutableURLRequest(url: url)
            let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
                var message = ""

                if error != nil {
                    print(error!)
                } else {
                    if let unwrappedData = data {
                        let dataString = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue)

                        var stringSeparator = "<span class=\"noprint ForceAgeToShow\">(age&#160;"

                        if let contentArray = dataString?.components(separatedBy: stringSeparator) {
                            if contentArray.count > 1 {
                                stringSeparator = ")</span>"

                                let newContentArray = contentArray[1].components(separatedBy: stringSeparator)

                                if newContentArray.count > 1 {
                                    message = newContentArray[1]
                                    print(message)
                                }
                            }
                        }
                    }
                }

                if message == "" {
                    message = "Please Enter a Name"
                }

                DispatchQueue.main.sync(execute: {
                    self.resultLabel.text = message
                })
            }

            task.resume()
        } else {
            resultLabel.text = "Age is unavailable at this time. Please try again later."
        }
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }

人的年龄应作为结果标签中的结果。

1 个答案:

答案 0 :(得分:0)

检查我的更新代码。我发现请求正常进行并打印响应。 (使用dataString检查print语句)。它失败的地方在响应之后。我收到“找不到年龄内容2”。

import UIKit

if let url = URL(string: "https://en.wikipedia.org/wiki/Bill_Gates") {
    let request = NSMutableURLRequest(url: url)
    let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
        var message = ""

        if error != nil {
            print(error!)
        } else {
            if let unwrappedData = data {
                let dataString = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue)

                print(dataString) // checks whether the actual request went through or not.

                var stringSeparator = "<span class=\"noprint ForceAgeToShow\">(age&#160;"

                if let contentArray = dataString?.components(separatedBy: stringSeparator) {
                    if contentArray.count > 1 {
                        stringSeparator = ")</span>"

                        let newContentArray = contentArray[1].components(separatedBy: stringSeparator)

                        if newContentArray.count > 1 {
                            message = newContentArray[1]
                            print(message)
                        } else {
                            print("NO age content found 1")
                        }
                    } else {
                        print("NO age content found 2")
                    }
                } else {
                    print("NO age component found")
                }
            }
        }

        if message == "" {
            message = "Please Enter a Name"
        }

        DispatchQueue.main.sync(execute: {
           print(message)
        })
    }

    task.resume()
}

对于您而言,如果未打印答复,建议您检查info.plist中提供的Internet许可。

检查此链接以获取更多信息。

https://guides.codepath.com/ios/Internet-Permissions