我试图在POST,GET等后台进行调用,以便在didReceiveRemoteNotification方法中更精确,因为它们开始作为推送通知到达.我的问题是,在我打开应用程序之前,所有Alamofire.request都不会在后台模式中调用.我现在有
我试图打开一个会话,但它不会使请求工作.
Alamofire.Manager(configuration: configuration).request(.GET,url,parameters: nil) .responseJSON { (_,_,JSON,_) in //println(JSON) println(JSON) REST OF THE CODE
虽然方法说“后台配置”,但它实际上意味着网络会话配置为
to allow for interruptions and continuation上传/下载.您需要做的是延长应用程序的执行时间,使其即使在后台运行一段时间
原文链接:https://www.f2er.com/swift/319245.html有beginBackgroundTaskWithExpirationHandler:这是专门为此而设计的.当您使用它时,您将获得更多的时间来执行您需要的任何内容(在此限制之后,无论如何,您的应用程序将被终止,现在您的应用程序立即终止).
您可以编写以下方法:
func beginBackgroundTask() -> UIBackgroundTaskIdentifier { return UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({}) } func endBackgroundTask(taskID: UIBackgroundTaskIdentifier) { UIApplication.sharedApplication().endBackgroundTask(taskID) }
当您想要使用它时,您只需在开始/完成下载调用时简单地开始/结束任务:
// Start task let task = self.beginBackgroundTask() // Do whatever you need,like download of the images self.someBackgroundTask() ... // End task once everything you need to do is done self.endBackgroundTask(task)
希望能帮助到你!
编辑1:
如果问题是您的下载方法永远不会被调用,那么这意味着您没有在通知有效负载中发送正确的数据:
For a push notification to trigger a download operation,the notification’s payload must include the content-available key with its value set to 1. When that key is present,the system wakes the app in the background (or launches it into the background) and calls the app delegate’s application:didReceiveRemoteNotification:fetchCompletionHandler: method. Your implementation of that method should download the relevant content and integrate it into your app. 07001