首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用NSURLSession背景配置检测DNS故障?

用NSURLSession背景配置检测DNS故障?
EN

Stack Overflow用户
提问于 2015-07-07 22:40:52
回答 1查看 980关注 0票数 6

在使用带有后台配置的NSURLSession时,是否有可能检测DNS查找失败?

如果使用默认配置和完成处理程序,则错误参数中报告DNS解析失败。但是,如果我使用后台配置,则不会报告失败,也不会调用委托方法。

NSURLSessionTaskDelegate协议的苹果文档说:

服务器错误不通过错误参数报告。您的委托通过错误参数接收的唯一错误是客户端错误,例如无法解析主机名或连接到主机。

这意味着我应该在那里看到DNS失败消息。下面的代码说明了这种行为。我在iOS 8.4上进行了测试,在设备和模拟器上进行了9的测试,并搜索了开发论坛,因此并使用了流行的搜索引擎,但结果却是空的。我肯定我错过了一些很简单的东西。

代码语言:javascript
运行
复制
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, NSURLSessionTaskDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


        let bgsesh = NSURLSession(configuration: NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("foobarbazqwux"), delegate: self, delegateQueue: nil)
        let dfsesh = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: nil, delegateQueue: nil)

        let url = NSURL(string: "http://foobarbaz.qwzx")!
        let a = dfsesh.downloadTaskWithURL(url) { (url:NSURL?, resp:NSURLResponse?, err:NSError?) -> Void in
            print(err)

            /* 
            Optional(Error Domain=NSURLErrorDomain Code=-1003
            "A server with the specified hostname could not be found." 
            UserInfo=0x146e8050 {
                NSErrorFailingURLStringKey=http://foobarbaz.qwzx/,
                _kCFStreamErrorCodeKey=8,
                NSErrorFailingURLKey=http://foobarbaz.qwzx/, 
                NSLocalizedDescription=A server with the specified hostname could not be found., 
                _kCFStreamErrorDomainKey=12, 
                NSUnderlyingError=0x14693ab0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1003.)"
            })
            */
        }
        a.resume()

        let b = bgsesh.downloadTaskWithURL(url)
        b.resume()

        return true
    }
    func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
        // never runs, DNS failure is not reported for background config :-(
        print("didCompleteWithError: \(task.taskIdentifier) \(error)")
    }
    // ... other delegate methods ...
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-10 14:41:25

设置URLSessionConfiguration.timeoutIntervalForResource

资源超时间隔控制在放弃之前等待整个资源传输的时间(以秒为单位)。 默认值为7天。

例如,

代码语言:javascript
运行
复制
let conf = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("foobarbazqwux")
conf.timeoutIntervalForResource = 60    // seconds

let session = NSURLSession(configuration: conf, delegate: self, delegateQueue: nil)

在这种情况下,系统将重新尝试连接并调用委托,如果它在60秒后仍在运行。

在@algrid的注释后面编辑:

此值应考虑到资源的传输时间。因此,它取决于下载/上传的资源大小和网络速度。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31280619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档