简体   繁体   English

具有后台配置的NSURLSession和被用户杀死的应用

[英]NSURLSession with background configuration and app killed by user

This is the scenario: 这是方案:

  • NSURLSession with background Configuration NSURLSession具有后台配置
  • Download or upload task start with Bad or No Internet Connection. 下载或上传任务从“互联网连接不良”或“没有互联网连接”开始。
  • User close the App. 用户关闭应用程序。
  • If iOS get Internet Connection will star session task. 如果iOS获得Internet连接,则将为会话任务添加星号。 However, 然而,
  • With task still waiting for Internet. 任务仍在等待Internet。
  • User kills the App 用户杀死了应用程序
  • System cancel all pending tasks 系统取消所有待处理任务

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.

相关问题 被用户杀死后,后台应用刷新会启动应用进行刷新吗? - Will Background App Refresh launch app to do refresh after it is killed by user? 检测应用程序何时被用户在后台状态 iOS 中杀死 - Detect when app killed by user in background state iOS 如果应用程序从任务管理器中被杀死,NSUrlSession是否继续文件传输? - Does NSUrlSession continue file transfer if the app is killed from task manager? 推送通知后台被用户杀死 - Push notification background killed by the user 应用终止时的NSURLSession后台会话回调 - NSURLSession background session callbacks on app termination 应用在iOS 7中后台运行时被杀死 - App be killed in the background running in iOS 7 iOS 6-在后台重新启动被终止的应用程序 - iOS 6 - Relaunch the killed app in background 具有后台会话配置的NSURLSession在没有连接时不返回错误 - NSURLSession with background session configuration is not returning an error when there is no connection 在NSURLSession(Background Session)事件的情况下如何测试后台应用启动? - How to test Background App Launch in case of NSURLSession(Background Session) event? 当用户从iOS的后台应用程序杀死Cordova应用程序时清除本地存储 - Clear Local storage when user killed the Cordova app from background apps in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM