大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Swift同步下载数据

戴维营教育代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//HTTP/HTTPS
        //URL: [url]http://pic.baike.soso.com/p/20120424/bki-20120424022918-1539307851.jpg[/url]
         
        //创建一个URL对象
        var url = NSURL(string: "/upload/otherpic73/203912.jpg")
        //创建一个HTTP请求
        var request = NSURLRequest(URL: url)
        //发送同步请求,开始下载数据
         
        var response: NSURLResponse?
        var error: NSError?
        var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)
        println("\(response):\(error)")
         
        var httpResponse = response as NSHTTPURLResponse
        //数据下载完成
        if httpResponse.statusCode == 200 {
            println("下载了\(data.length)字节")
            data.writeToFile("/Users/cheetah/Desktop/test.jpg", atomically: true)
        }
        else {
            println("下载失败: \(httpResponse.statusCode)")
        }

当前题目:Swift同步下载数据
本文链接:http://dzwzjz.com/article/pijeeg.html
在线咨询
服务热线
服务热线:028-86922220
TOP