iOS开发——网络编程Swift篇&(四)异步Get方式

时间:2023-03-08 15:50:08
iOS开发——网络编程Swift篇&(四)异步Get方式

异步Get方式

     // MARK: - 异步Get方式
     func asynchronousGet()
     {
         //创建NSURL对象
         var url:NSURL! = NSURL(string: "http://m.weather.com.cn/data/101010100.html")

         //创建请求对象
         var urlRequest : NSURLRequest = NSURLRequest(URL: url, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: )

         //连接服务器
         var connection = NSURLConnection(request: urlRequest, delegate: self)
     }

     // MARK: - NSURLConnectionDelegate : NSObjectProtocol {
     func connection(connection: NSURLConnection, didFailWithError error: NSError)
     {
         //请求失败
     }
     func connectionShouldUseCredentialStorage(connection: NSURLConnection) -> Bool
     {
         //连接应使用证书存储
         return true
     }
     func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge)
     {
         //发送请求验证
     }
     func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool
     {
         //可以验证的保护空间
         return true
     }
     func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge)
     {
         //获得认证
     }
     func connection(connection: NSURLConnection, didCancelAuthenticationChallenge challenge: NSURLAuthenticationChallenge)
     {
         //取消认证
     }