即使在应用程序被终止/终止/挂起时,我仍然试图获得位置更新,即使在所有状态下也是如此。我已经在xcode中启用后台获取并实现了以下代码(使用参考“Capture location in all states app”)。但是当我终止应用程序时,它会在AppDelegate类上发出红线。我不明白这里有什么问题。我在这里使用了“Getting location for an iOS app when it is in the background and even killed”这个问题的解决方案,但是它不能在ios 9中工作。请帮助我或告诉我另一种解决方案。即使应用程序被杀害/终止,位置更新
更新的代码 -
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var window: UIWindow?
var locationManager: CLLocationManager?
var significatLocationManager : CLLocationManager?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
if(UIApplication.sharedApplication().backgroundRefreshStatus == UIBackgroundRefreshStatus.Available){
print("yessssss")
}else{
print("noooooo")
}
if let launchOpt = launchOptions{
if (launchOpt[UIApplicationLaunchOptionsLocationKey] != nil) {
self.significatLocationManager = CLLocationManager()
self.significatLocationManager?.delegate = self
self.significatLocationManager?.requestAlwaysAuthorization()
if #available(iOS 9.0, *) {
self.significatLocationManager!.allowsBackgroundLocationUpdates = true
}
self.significatLocationManager?.startMonitoringSignificantLocationChanges()
}else{
self.locationManager = CLLocationManager()
self.locationManager?.delegate = self
self.locationManager?.requestAlwaysAuthorization()
if #available(iOS 9.0, *) {
self.locationManager!.allowsBackgroundLocationUpdates = true
}
self.locationManager?.startMonitoringSignificantLocationChanges()
}
}else{
self.locationManager = CLLocationManager()
self.locationManager?.delegate = self
self.locationManager?.requestAlwaysAuthorization()
if #available(iOS 9.0, *) {
self.locationManager!.allowsBackgroundLocationUpdates = true
}
self.locationManager?.startMonitoringSignificantLocationChanges()
}
return true
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
let locationArray = locations as NSArray
let locationObj = locationArray.lastObject as! CLLocation
let coord = locationObj.coordinate
}
func applicationDidEnterBackground(application: UIApplication) {
if self.significatLocationManager != nil {
self.significatLocationManager?.startMonitoringSignificantLocationChanges()
}else{
self.locationManager?.startMonitoringSignificantLocationChanges()
}
}
可能重复:http://stackoverflow.com/questions/30396367/getting-location-for-an-ios-app-when-it-is-in-the-background甚至杀了 –
*“它给AppDelegate类上的红线”* - 这是最糟糕和最不有用的错误描述。请在这里发布错误消息。如果您通过Xcode运行应用程序,如果您强制终止应用程序,则显示“红线”是正常的。 – luk2302
是的,我强制终止应用程序,并显示此红线错误,但我没有得到终止应用程序的位置更新。这里的问题是什么? – Kirti