问题
Can I return the server response from any function to its calling function using any of the Network API like AFNetworking, MKNetworkKit etc, in iPhone.
Currently I am using httpGet function of NSURLRequest. I made the following function but I am not able to return the server response to it calling function. Please help me.
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:
I do not know how your LearnNSURLSession
class works, so I cannot suggest a solution with that. But here is how to find the returned headers and status, together with the returned data and any error information, from an NSURLConnection, documented 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.") }
})
While that documentation suggests that the response is an NSURLResponse
, this page makes clear that you will get a NSHTTPURLResponse
which contains all the headers and status.
If you need a synchronous version, that will stop the function until it gets a result, you can use this call instead, like this:
var response:NSURLResponse?
var e: NSError?
NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &e)
debugPrintln(response)
That will allow you to return a meaningful result to the caller.
来源:https://stackoverflow.com/questions/29414140/network-api-to-return-https-response-in-iphone