本文介绍了Swift iOS:收到远程通知后,Closure不会在后台运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用闭包来获取当前的GPS位置。此代码在应用程序处于前台时完美运行(新的远程通知和已获得位置均打印到控制台),但当应用程序位于后台时,只有新的远程通知(并且没有位置错误)被打印到控制台。因此,我现在还没有设备的GPS位置,这对我的应用程序的功能非常重要。



任何帮助将不胜感激。
Thanks。



我的Info.plist文件中有'必需的背景模式';

 位置更新的应用程序注册
应用程序从网络下载内容
应用程序下载内容以响应推送通知

我的应用程序还可以随时访问位置,包括背景(在代码中的其他位置成功测试过):NSLocationAlwaysUsageDescription位于Info.plist文件中



在我的AppDelegate文件中:

  func application(application:UIApplication! ,didReceiveRemoteNotification userInfo:NSDictionary!){

println(新的远程通知)
var通知:NSDictionary = userInfo as NSDictionary

var loc:LocationManager?
loc = LocationManager()


loc!.fetchWithCompletion {位置,错误

//获取位置或错误
if让myloc = location {

var lat = myloc.coordinate.latitude as Double
var lon = myloc.coordinate.longitude as Double

println(Got location )

} else if if err = error {

println(err.localizedDescription)

}
loc = nil


$ b


解决方案

如果app不在前台,请确保使用 beginBackgroundTaskWithExpirationHandler 请求一点时间来完成请求,然后调用 endBackgroundTask 完成后。



有关更多信息,请参阅一章。

I am writing an iOS app that requires the device's GPS loction to be updated when a push notification is received.

I use a closure to get the current GPS location. This code runs perfectly when the app is in the foreground (Both "New remote notification" and "Got location" is printed to console), but when the app is in the background, only "New remote notification" (and no location error) is printed to console. As a result I don't have the GPS location of the device at this point, which is very important for my app's functionality.

Any help would be greatly appreciated.Thanks.

I have in my Info.plist file for 'Required background modes';

 App registers for location updates
 App downloads content from the network
 App downloads content in response to push notifications

My app also has access to location at all times, including the background (successfully tested at other points in the code): NSLocationAlwaysUsageDescription is in the Info.plist file

In my AppDelegate file:

    func application(application: UIApplication!, didReceiveRemoteNotification userInfo:NSDictionary!) {

    println("New remote notification")
    var notification:NSDictionary = userInfo as NSDictionary

    var loc: LocationManager?
    loc = LocationManager()


    loc!.fetchWithCompletion {location, error in

        // fetch location or an error
        if let myloc = location {

            var lat = myloc.coordinate.latitude as Double
            var lon = myloc.coordinate.longitude as Double

            println("Got location")

        } else if let err = error {

            println(err.localizedDescription)

        }
        loc = nil
    }

}
解决方案

If app is not in foreground, make sure to ask for a little time to complete the request with beginBackgroundTaskWithExpirationHandler and then call endBackgroundTask when done.

For more information, see the Executing Finite Length Tasks in the Background Execution chapter of the App Programming Guide for iOS.

这篇关于Swift iOS:收到远程通知后,Closure不会在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 20:38