Swift - 使用NSURL进行数据的提交和获取(POST与GET)
使用Swift进行iOS开发时,不可避免的要进行远程的数据获取和提交。
其数据请求的方式既可能是POST也可能是GET。同不管是POST还是GET又可以分为同步请求和异步请求。
下面通过四个例子来进行演示。
(2)异步请求
(2)异步请求
1,使用POST方式提交数据(用户id和分数)
(1)同步请求
//保存分数 func saveScore(score:Int, userid:String) { let urlString:String = "http://www.hangge.com/" var url:NSURL! url = NSURL(string:urlString) let request = NSMutableURLRequest(URL:url) let body = "score=\(score)&user=\(userid)" //编码POST数据 let postData = body.dataUsingEncoding(NSUTF8StringEncoding) //保用 POST 提交 request.HTTPMethod = "POST" request.HTTPBody = postData //响应对象 var response:NSURLResponse? do{ //发出请求 let received:NSData? = try NSURLConnection.sendSynchronousRequest(request, returningResponse: &response) let datastring = NSString(data:received!, encoding: NSUTF8StringEncoding) print(datastring) }catch let error as NSError{ //打印错误消息 print(error.code) print(error.description) } }
(2)异步请求
import UIKit class ScoreController:NSObject, NSURLConnectionDataDelegate { //保存分数 func saveScore(score:Int, userid:String) { let urlString:String = "http://www.hangge.com/savescore.php" var url:NSURL! url = NSURL(string:urlString) let request = NSMutableURLRequest(URL:url) let body = "score=\(score)&user=\(userid)" //编码POST数据 let postData = body.dataUsingEncoding(NSASCIIStringEncoding) //保用 POST 提交 request.HTTPMethod = "POST" request.HTTPBody = postData var conn:NSURLConnection! conn = NSURLConnection(request: request,delegate: self) conn.start() print(conn) } func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) { print("请求成功!"); print(response) } func connection(connection: NSURLConnection, didReceiveData data: NSData) { print("请求成功1!"); let datastring = NSString(data:data, encoding: NSUTF8StringEncoding) print(datastring) } func connectionDidFinishLoading(connection: NSURLConnection) { print("请求成功2!"); } }
2,使用GET方式获取数据(用户id对应的分数)
(1)同步请求
//获取分数 func getScore(user:String){ let urlString:String="http://www.hangge.com/getscore.php?user=\(user)" var url:NSURL! url = NSURL(string:urlString) let request = NSMutableURLRequest(URL:url) request.HTTPMethod = "GET" //响应对象 var response:NSURLResponse? do{ //发出请求 let received:NSData? = try NSURLConnection.sendSynchronousRequest(request, returningResponse: &response) let datastring = NSString(data:received!, encoding: NSUTF8StringEncoding) print(datastring) }catch let error as NSError{ //打印错误消息 print(error.code) print(error.description) } }
(2)异步请求
import UIKit class ScoreController:NSObject, NSURLConnectionDataDelegate { //获取分数 func getScore(user:String) { let urlString:String="http://hangge.com/getscore.php?user=\(user)" var url:NSURL! url = NSURL(string:urlString) let request = NSMutableURLRequest(URL:url) request.HTTPMethod = "GET" var conn:NSURLConnection! conn = NSURLConnection(request: request,delegate: self) conn.start() print(conn) } func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) { print("请求成功!"); print(response) } func connection(connection: NSURLConnection, didReceiveData data: NSData) { print("请求成功1!"); let datastring = NSString(data:data, encoding: NSUTF8StringEncoding) print(datastring) //解析 JSON 数据 do { let json : AnyObject! = try NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.AllowFragments) let score = json.objectForKey("score") as! Int print(score) }catch let error as NSError{ //打印错误消息 print(error.code) print(error.description) } } func connectionDidFinishLoading(connection: NSURLConnection) { print("请求成功2!"); } }
请问航哥怎么将手机app中的数据传给某个特定的服务器,或者如何获得服务器中特定的资源?有没有什么实例?
麻烦航哥更新一下这个吧NSURLConnection 谢谢了十分感谢