【发布时间】:2016-02-14 01:50:39
【问题描述】:
我正在制作一个基本的“hello world”iOS 应用。我在云中有一个 ubuntu 服务器,我想从 iOS 应用程序中查询它。我了解服务器需要安全,即需要通过https请求访问,并且服务器上的证书需要“信任”。
现在我想做的是覆盖这个要求。我有一个自签名证书,适用于我的服务器上的 https。当我使用 iOS 应用程序发出请求时,它给了我一些关于 NSURLErrorFailingURLPeerTrustErrorKey 的错误,甚至返回一行:NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?。
我知道这是一个常见问题,并且该网站上有很多关于如何处理它的帖子。我尝试了来自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 事件,并告诉它“是的,我想继续”。但是那段代码似乎没有被调用。发布请求已发送,但我收到有关不受信任证书的错误消息。有人可以帮我修复我的代码,以便我可以从我的服务器接受此证书吗?
【问题讨论】:
-
仅供参考,我最终只是购买了 SSL 证书,所有本机软件都可以完美地与任何解决方法配合使用。