[英]NSURLSession with background configuration and app killed by user
This is the scenario: 这是方案:
The Question 问题
It is possible to know when the user opens the app again that the tasks were cancelled? 是否有可能知道用户再次打开应用程序时任务已取消?
If yes, where? 如果是,在哪里?
This Answer says yes, it is possible, but I can not get any callback returning me an error. 这个答案说是的,这是可能的,但是我无法得到任何回调,使我返回错误。
I'm using Alamofire to handle all my Networking calls. 我正在使用Alamofire来处理我的所有联网呼叫。 However, I doubt that Alamofire will change the behavior.
但是,我怀疑Alamofire是否会改变行为。
Edit 1 编辑1
/// Networking manager with Background Session Configuration
static var backgroundManager: Alamofire.Manager = {
let configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.xxx.NetworkingManager.Identifier")
configuration.HTTPAdditionalHeaders = Manager.defaultHTTPHeaders
let backgroundManager = Alamofire.Manager(configuration: configuration)
return backgroundManager
}()
Kudos to Rob because he showed me the right path. 感谢Rob,因为他向我展示了正确的道路。
So after the user kills the app, the system cancels all the pending tasks. 因此,在用户终止应用程序后,系统会取消所有待处理的任务。
You can see that with the system.log: 您可以在system.log中看到:
Simulator/Debug/Open System Log...
模拟器/调试/打开系统日志...
How to catch what was already ongoing? 如何捕捉已经发生的事情?
Instantiate again your Background NSURLSession
. 再次实例化您的Background
NSURLSession
。 Do it elsewhere, but I'll do it in AppDelegate for this example. 在其他地方进行此操作,但在此示例中,我将在AppDelegate中进行处理。
The system knows (thanks to the identifier) that it is the same Background Session that before so it maps the pending tasks. 系统知道(由于标识符)与之前的后台会话相同,因此它映射了挂起的任务。
Then retrieve all the tasks. 然后检索所有任务。 The canceled tasks are still there
被取消的任务仍然存在
The tasks will have a error that you can check. 这些任务将有一个您可以检查的错误。
Error Domain=NSURLErrorDomain Code=-999 "(null)"
UserInfo={NSErrorFailingURLStringKey=http://your.api.com/url,
NSURLErrorBackgroundTaskCancelledReasonKey=0,
NSErrorFailingURLKey=http://your.api.com/url}
Also, with the tasks, you will get the Request URL, so you can map your app requests and do something. 另外,通过这些任务,您将获得“请求URL”,以便您可以映射应用程序请求并执行某些操作。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// This is the code for Swift 2.x. In Swift 3.x this call is a bit different.
NetworkingManager.backgroundManager.session.getTasksWithCompletionHandler { (data, upload, download) in
for task in data {
NSLog("\(task.error)")
}
for task in upload {
NSLog("\(task.error)")
}
for task in download {
NSLog("\(task.error)")
let reason = task.error?.userInfo[NSURLErrorBackgroundTaskCancelledReasonKey] as? Int
let code = task.error?.code
if reason == NSURLErrorCancelledReasonUserForceQuitApplication &&
code == NSURLErrorCancelled {
NSLog("\(task.originalRequest)")
NSLog("\(task.currentRequest?.URL)")
}
}
}
}
NSURLErrorCancelledReasonUserForceQuitApplication
-> The operation was canceled because the user forced the app to quit. NSURLErrorCancelledReasonUserForceQuitApplication
> 操作被取消,因为用户强制退出应用程序。
So we are on the right track. 所以我们走在正确的轨道上。 If someone has a better solution, please share!
如果有人有更好的解决方案,请分享! I do not really like the mapping solution of my requests urls.
我不太喜欢我的请求网址的映射解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.