标签:
异步Get方式
1 // MARK: - 异步Get方式 2 func asynchronousGet() 3 { 4 //创建NSURL对象 5 var url:NSURL! = NSURL(string: "http://m.weather.com.cn/data/101010100.html") 6 7 //创建请求对象 8 var urlRequest : NSURLRequest = NSURLRequest(URL: url, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: 10) 9 10 //连接服务器 11 var connection = NSURLConnection(request: urlRequest, delegate: self) 12 } 13 14 15 // MARK: - NSURLConnectionDelegate : NSObjectProtocol { 16 func connection(connection: NSURLConnection, didFailWithError error: NSError) 17 { 18 //请求失败 19 } 20 func connectionShouldUseCredentialStorage(connection: NSURLConnection) -> Bool 21 { 22 //连接应使用证书存储 23 return true 24 } 25 func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) 26 { 27 //发送请求验证 28 } 29 func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool 30 { 31 //可以验证的保护空间 32 return true 33 } 34 func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge) 35 { 36 //获得认证 37 } 38 func connection(connection: NSURLConnection, didCancelAuthenticationChallenge challenge: NSURLAuthenticationChallenge) 39 { 40 //取消认证 41 }
标签:
原文地址:http://www.cnblogs.com/iCocos/p/4559623.html