定期更新位置背景Swift 3.0 iOS 10
我做一个应用程序,当它进入后台我需要发送到服务器一些周期性的数据。 为了从后台唤醒,我使用后台更新的重要位置(较低的电池)。 我注意到,当从wifi / 3g手机细胞或3G时改变手机塔的位置更新,但我的问题是,如果用户不移动(然后手机塔不改变)的位置不更新和应用程序doest醒来,然后我不能发送数据到服务器。
你知道一些方法来解决这个问题吗?
我在AppDelegate文件中执行所有这些操作:
class AppDelegate: UIResponder, UIApplicationDelegate , CLLocationManagerDelegate{ var manager = CLLocationManager() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { manager.desiredAccuracy = kCLLocationAccuracyBest manager.delegate = self manager.requestAlwaysAuthorization() manager.allowsBackgroundLocationUpdates = true return true } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){ sendDataToServer() } func applicationDidEnterBackground(_ application: UIApplication) { manager.startMonitoringSignificantLocationChanges() } }
重要的位置变化事件仅在存在位置变化(即,小区塔变化)时触发。 如果您需要定期下载或上传数据,则应该使用后台获取而不是位置更改来唤醒应用程序:
application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
然后处理
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)