我正在制作一款应用程序,在后台运行时,我需要定期向服务器发送一些数据。为了唤醒它,我使用了后台更新显著位置(降低电量消耗)。我注意到当手机从wifi/3g切换或3g更改基站时,位置会更新,但是如果用户不移动(并且基站不改变),位置就不会更新,应用程序就无法唤醒,然后我就无法向服务器发送数据。
你知道解决这个问题的方法吗?
我都在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()
}
}