用于在iPhone中返回HTTPS响应的网络API

时间:2015-04-02 13:25:37

标签: swift https nsurlrequest xcode-6.2

我是否可以使用iPhone中的任何网络API(如AFNetworking,MKNetworkKit等)将服务器响应从任何功能返回到其调用功能。

目前我正在使用NSURLRequest的httpGet功能。我做了以下功能,但我无法将服务器响应返回给它调用函数。请帮帮我。

func connserv(jsonString:NSDictionary) -> NSDictionary{
    var abc: NSDictionary?
    // This is the action performed when clicked on the Connect button on the connectivity screen
    println("------------------Function connserv")

    let prefs = NSUserDefaults.standardUserDefaults()
    var IP: AnyObject = prefs.objectForKey("IP")!
    var port: AnyObject = prefs.objectForKey("Port")!
    println("IP in Connection : \(IP)")
    println("port in Connection : \(port)")
    prefs.synchronize()
    //var learn = LearnNSURLSession()
    let localizedModel = UIDevice.currentDevice().localizedModel
    let model = UIDevice.currentDevice().model
    let devicesystemVersion = UIDevice.currentDevice().systemVersion

    println("HTTP request jsonString : \(jsonString)")
    var request = NSMutableURLRequest(URL: NSURL(string: "https://\(IP):\(port)/")!)

    var response: NSURLResponse?
    var error: NSError?

    //println("HTTP request jsonString : \(jsonString)")

    var err: NSError?
    request.HTTPBody = NSJSONSerialization.dataWithJSONObject(jsonString, options: nil, error: &err)
    request.HTTPMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.setValue("application/json", forHTTPHeaderField: "Accept")
    // send the request
    var learn = LearnNSURLSession()
    println("HTTP request : \(request)")
    learn.httpGet(request) {
        (resultString, error) -> Void in
        if error != nil
        {
            println("completion block")

        }
        else
        {
            let data = (resultString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
            var er: NSError?
            let JSONdata: AnyObject = NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers,error: &er)!

            let abc: AnyObject = NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves, error:&er)!

            println("abc : \(abc)")
            println("JSONdata : \(JSONdata)")

            learn.callback(result: resultString, error: error)
        }
    }
    //return abc!;
}

1 个答案:

答案 0 :(得分:0)

我不知道你的LearnNSURLSession课是如何运作的,所以我无法提出解决方法。但是这里是如何从NSURLConnection中找到返回的标题和状态,以及返回的数据和任何错误信息,记录在案here

    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.currentQueue(), completionHandler: {
        response, data, error in
        if let response = response as? NSHTTPURLResponse {
            debugPrintln(response.allHeaderFields)
            let statusCode = response.statusCode
            debugPrintln("statusCode: \(statusCode): \(NSHTTPURLResponse.localizedStringForStatusCode(statusCode))")
        } else { println("That's odd.") }
    })

虽然该文档表明回复是NSURLResponse,但this page明确表示您将获得包含所有标题和状态的NSHTTPURLResponse

如果您需要一个同步版本,这将停止该功能直到获得结果,您可以使用this call instead,如下所示:

var response:NSURLResponse?
var e: NSError?
NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &e)
debugPrintln(response)

这将允许您将有意义的结果返回给调用者。