NSURLConnection sendSynchronousRequest不起作用

时间:2015-03-26 14:06:38

标签: ios nsurlconnection

知道为什么sendSynchronousRequest不起作用,并且返回nil。错误和响应也是零。

let url = NSURL(fileURLWithPath: "http://google.com")!
let ur = NSMutableURLRequest(URL: url)
let response: AutoreleasingUnsafeMutablePointer<NSURLResponse?> = nil
let errorPtr = NSErrorPointer()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
    let data = NSURLConnection.sendSynchronousRequest(ur, returningResponse: response, error: errorPtr)
    if errorPtr != nil {
        var error: NSError = errorPtr.memory!
    }
})

更新

我尝试了异步方式:

var oq = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(ur, queue: oq, completionHandler: {response, data, error in

    let ii = 7
})

我在这里收到错误:

  

(lldb)po错误   错误域= NSURLErrorDomain代码= -1100&#34;在此服务器上找不到请求的URL。&#34; UserInfo = 0x1567f9d0 {NSErrorFailingURLStringKey = file:/// http:/google.com,NSErrorFailingURLKey = file:/// http:/ google.com,NSLocalizedDescription =在此服务器上找不到请求的URL。,NSUnderlyingError = 0x15691880&# 34;在此服务器上找不到请求的URL。&#34;}

奇怪的谷歌不可用?

1 个答案:

答案 0 :(得分:3)

您正在使用fileURLWithPath并返回该网址 文件:///private/var/folders/xs/6k9v2g217155wr9vgb3xrg_80000gn/T/com.apple.dt.Xcode.pg/containers/com.apple.dt.playground.stub.iOS_Simulator.MyPlayground-F81D60A5-6797-4BEB-8AB9 -2D156E2B6771 / HTTP:/google.com

您需要的是let url = NSURL(string: "http://google.com")! 并返回网址:
http://google.com