iOS 第一个应用程序,接受 hello world 的不受信任证书

标签 ios swift http-post

我正在制作一个基本的“hello world”iOS 应用程序。我在云中有一个 ubuntu 服务器,我想从 iOS 应用程序查询它。我理解服务器需要安全,即需要通过https请求访问,并且服务器上的证书需要“可信”。

现在我想做的是覆盖这个要求。我有一个自签名证书,适用于我的服务器上的 https。当我使用 iOS 应用程序发出请求时,它会给出一些有关 NSURLErrorFailingURLPeerTrustErrorKey 的错误,甚至返回一行内容:NSLocalizedRecoverySuggestion=您是否仍要连接到服务器? .

我知道这是一个常见问题,并且该网站上有很多关于如何处理该问题的主题。我尝试了 this post 中的一段代码。我将这段添加到我的代码中:

    func URLSession(session: NSURLSession, task: NSURLSessionTask, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
        if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust{
            let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
            completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential,credential);
        }
    }

我的整个 ViewController 代码在这里:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, NSURLSessionDelegate {
    // MARK: Properties
    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var mealNameLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Handle the text field’s user input through delegate callbacks.
        nameTextField.delegate = self
    }

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

    // MARK: UITextFieldDelegate
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        // Hide the keyboard.
        textField.resignFirstResponder()
        return true
    }

    func textFieldDidEndEditing(textField: UITextField) {
        mealNameLabel.text = nameTextField.text
    }

    // MARK: Actions
    @IBAction func setDefaultLabelText(sender: UIButton) {
        mealNameLabel.text = "Default Text"
        post_request()
    }

    func post_request(){
        let request = NSMutableURLRequest(URL: NSURL(string: "https://54.164.XXX.XX/post_script.php")!)
        request.HTTPMethod = "POST"
        let postString = "id=13&name=Jack"
        request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)

        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
            data, response, error in

            if error != nil {
                print("error=\(error)")
                return
            }

            print("response = \(response)")

            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("responseString = \(responseString)")
        }
        task.resume()
    }

    func URLSession(session: NSURLSession, task: NSURLSessionTask, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
        if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust{
            let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
            completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential,credential);
        }
    }

}

看起来这个想法是从连接中获取challenge事件,并告诉它“是的,我想继续”。但那段代码似乎没有被调用。发布请求已发送,但我收到有关不受信任的证书的错误消息。有人可以帮我修复我的代码,以便我可以从我的服务器接受此证书吗?

最佳答案

您需要使 ViewController NSURLSessionDelegate 接收回调并将 session 的委托(delegate)设置为您的 Controller ,如下所示:

let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
...
let task = session.dataTaskWithRequest(request) { data, response, error in

}

task.resume()

关于iOS 第一个应用程序,接受 hello world 的不受信任证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33676516/

相关文章:

ios - Safari 显示某些网站的空白页面 IOS 12

string - 无法将类型 'GeoPoint?' 的值分配给类型 'AnyObject?' 的值

ios - 如何从 iPhone 中的项目 test-Info.plist 中检索标签中的 Bundle Version?

ios - 自定义 UISearchBar

ios - 如何获取 ARKit 特征点的 UIKit 坐标?

ios - 如何根据点击的 UITableView 行更改方法的参数?

r - 发送数据时使用POST下载R中的文件

php - 在表单中发送 POST 和 GET

php - 使用 CPP 手动构建 POST 请求并提交 php 表单

ios - 与在设备上测试相比,AIR for iOS 应用程序在打包时表现不同