知道为什么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“在此服务器上找不到请求的URL。” UserInfo = 0x1567f9d0 {NSErrorFailingURLStringKey = file:/// http:/google.com,NSErrorFailingURLKey = file:/// http:/google.com,NSLocalizedDescription =在此服务器上找不到请求的URL。NSUnderlyingError= 0x15691880“在此服务器上找不到请求的URL。“}
奇怪的谷歌不可用?
最佳答案
您正在使用fileURLWithPath
,它将返回URL
文件:///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")!
,它将返回URL:
http://google.com
关于ios - NSURLConnection sendSynchronousRequest不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29280580/