ios - NSURLConnection sendSynchronousRequest不起作用

标签 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“在此服务器上找不到请求的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/

相关文章:

objective-c - NSURLConnection 使用 ARC 发送同步请求

swift - 在快速启动屏幕期间使用网络服务

ios - MonoTouch : How to download pdf incrementally as indicated in the Apple slides "Building Newsstand Apps", session 504?

ios - 为 iOS 发送多个 HTTP Post 请求的最快方法?

ios - 如何在 Storyboard中使用 NSURLConnection 下载文件

ios - UITouch Force on touchesBegan

iphone - 在后台线程上将 SVG XML NSString 转换为 UIImage

ios - 跳过一个通知,每天重复

ios - 在 RubyMotion 中捕获 UIView

ios - iOS 上可以使用相机追踪脸部吗?