gpt4 book ai didi

ios - 即使应用程序被终止或从 ios 中的后台删除,我如何继续更新我的位置?

转载 作者:搜寻专家 更新时间:2023-11-01 06:18:21 30 4
gpt4 key购买 nike

我的应用程序需要不断更新位置,即使应用程序被终止或从后台删除也是如此。它在前台甚至后台模式下都能正常工作。但在应用程序被杀死时无法正常工作。我试过一些代码。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

self.startLocationService()

if ((launchOptions?[UIApplicationLaunchOptionsLocationKey]) != nil) {
self.locationmanager = CLLocationManager()
self.startLocationService()
}

print("Location Updates started")


return true
}

func startLocationService()
{
self.locationmanager.requestWhenInUseAuthorization()
self.locationmanager.desiredAccuracy = kCLLocationAccuracyBest
self.locationmanager.allowsBackgroundLocationUpdates = true
self.locationmanager.requestAlwaysAuthorization()
self.locationmanager.delegate = self


if UIApplication.sharedApplication().backgroundRefreshStatus == .Available {
print("Background updates are available for the app.")

}
else if UIApplication.sharedApplication().backgroundRefreshStatus == .Denied {
print("The user explicitly disabled background behavior for this app or for the whole system.")

}
else if UIApplication.sharedApplication().backgroundRefreshStatus == .Restricted {
print("Background updates are unavailable and the user cannot enable them again. For example, this status can occur when parental controls are in effect for the current user.")

}
else
{
print("nothing")
}

self.locationmanager.startUpdatingLocation()
}



func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
// locationManager = CLLocationManager()
}

func applicationDidBecomeActive(application: UIApplication) {

self.startLocationService()
}

func applicationWillTerminate(application: UIApplication) {
self.locationmanager.startMonitoringSignificantLocationChanges()

}


func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("did update locateion called-->..")
let location:CLLocation = locations[locations.count-1]

print(location.coordinate.longitude)
print(location.coordinate.latitude)

let newTime : NSDate = NSDate()

let calendar: NSCalendar = NSCalendar.currentCalendar()
let flags = NSCalendarUnit.Second
let components = calendar.components(flags, fromDate: oldTime, toDate: newTime, options: [])


//Update locations to server
self.call_service_insertLocation("location", loc: location)

}


func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print(error)
}

最佳答案

希望对您有所帮助。您的应用可以在因某种原因(内存压力)终止时被唤醒。

但是当应用程序终止时,后台位置更新有限制。

这是 Apple 的位置和 map 编程指南中的注释。

If your app is terminated either by a user or by the system, the system doesn't automatically restart your app when new location updates arrive. A user must explicitly relaunch your app before the delivery of location updates resumes. The only way to have your app relaunched automatically is to use region monitoring or significant-change location service. However, when a user disables the Background App Refresh setting either globally or specifically for your app, the system doesn't relaunch your app for any location events, including significant change or region monitoring events. Further, while Background App Refresh is off your app won't receive significant change or region monitoring events even when it's in the foreground.

因此,要在后台接收位置更新,您应该为您的应用开启Background App Refresh设置(您可以在Settings/Your App/Background App Refresh 在你的 iPhone 中。)

此外,只有在应用程序终止时才会交付重大更改,例如您在此处提到的位置更改(我的意思是 kCLLocationAccuracyBest)可能不会唤醒您的应用程序。此外,您应该在您的应用委托(delegate)中重新启动位置管理器s application:didFinishLaunching:withOptions 方法来检索下一个重要的位置更改。还要确保在后台模式下检索位置时尽快返回,或者使用后台任务机制使应用程序在后台工作几分钟。 (2~3 分钟)。

关于ios - 即使应用程序被终止或从 ios 中的后台删除,我如何继续更新我的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39343033/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com