我们来了解另外一种方式进行网络请求:NSURLConnection.其实这种方式是比较直接的处理方式,更容易理解.
NSURLConnection的创建步骤:
1.首要创建一个URL地址
2.然后创建实例NSURL
3.将NSURL传递给NSURLRequest或者NSMutableURLRequest
4.最后将request传递给NSURLConnection,通过NSURLConnection进行请求操作
在这里我们来看看几种不同方式的处理NSURLConnection的请求
a.最基本的请求处理,这里我们使用的时NSURLConnection的异步发送请求,其中在执行完请求后,我们可以通过异步或同步处理数据.
func basicRequest(){ let urlstr = "http://www.wutongwei.com" let url = NSURL(string: urlstr) let urlrequest = NSURLRequest(URL: url!) let op:NSOperationQueue = NSOperationQueue() //通过NSURLConnection发送请求 NSLog("开始请求数据...") NSURLConnection.sendAsynchronousRequest(urlrequest, queue: op) { (response:NSURLResponse!, data:NSData!, error:NSError!) -> Void in NSLog("下载完成") println("this is request") //同步处理 // dispatch_sync(dispatch_get_main_queue(), { () -> Void in // // }) //异步处理 dispatch_async(dispatch_get_main_queue(), { () -> Void in self.label?.text = "下载完成......" }) } }
b.创建一个,可以处理网络请求超时的连接
func timeOutRequest(){ let urlstr = "http://www.wutongwei.com" let url = NSURL(string: urlstr) let urlrequest = NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataDontLoad, timeoutInterval: 5) let op:NSOperationQueue = NSOperationQueue() //通过NSURLConnection发送请求 NSLog("开始请求数据...") NSURLConnection.sendAsynchronousRequest(urlrequest, queue: op) { (response:NSURLResponse!, data:NSData!, error:NSError!) -> Void in NSLog("下载完成") println("this is request") dispatch_async(dispatch_get_main_queue(), { () -> Void in self.label?.text = "下载完成......" }) } }
c.自定义请求参数,这里我们需要使用NSMutableURLRequest
func customRequest(){ let urlstr = "http://www.wutongwei.com" let url = NSURL(string: urlstr) let urlrequest = NSMutableURLRequest(URL: url!) urlrequest.timeoutInterval = 10 NSURLConnection.sendAsynchronousRequest(urlrequest, queue: NSOperationQueue()) { (response, data, error) -> Void in NSLog("下载完成") } }
d.同步方式处理请求,同步请求,在体验上可能有所下降,当然这个根据自己的需求而定
func synRequest(){ let urlstr = "http://www.wutongwei.com" let url = NSURL(string: urlstr) let urlrequest = NSURLRequest(URL: url!) let data:NSData? = NSURLConnection.sendSynchronousRequest(urlrequest, returningResponse: nil, error: nil) NSLog("\(data)") }
e.为了处理同步请求上的体验,我们可以将请求加入到处理的优先队列
func synRequest2(){ let urlstr = "http://www.wutongwei.com" let url = NSURL(string: urlstr) let urlrequest = NSURLRequest(URL: url!) //不能通过NSOperationQueue来处理线程队列 let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0) dispatch_async(queue, { () -> Void in let data:NSData? = NSURLConnection.sendSynchronousRequest(urlrequest, returningResponse: nil, error: nil) NSLog("\(data)") }) }
f.通过NSURLConnection来处理POST请求,如果要发送数据,NSMutableURLRequest有一个属性HTTPBody
func postRequest(){ let urlstr = "http://www.wutongwei.com" let url = NSURL(string: urlstr) let urlrequest = NSMutableURLRequest(URL: url!) urlrequest.HTTPMethod = "POST" urlrequest.setValue("Tonway Browser", forHTTPHeaderField: "User-Agent") NSURLConnection.sendAsynchronousRequest(urlrequest, queue: NSOperationQueue()) { (response:NSURLResponse!, data:NSData!, error:NSError!) -> Void in NSLog("请求完成") } }
ok,NSURLConnection就先介绍到这里咯,需要更详细的相关操作,可以去官网查看API文档.