【发布时间】:2016-04-04 08:04:25
【问题描述】:
即使应用程序被终止/终止/暂停,我也在尝试获取所有州的位置更新。我在 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()
}
}
【问题讨论】:
-
“它在 AppDelegate 类上给出了一条红线” - 这是最糟糕和最没有帮助的错误描述之一。请在此处发布错误消息。如果您通过 Xcode 运行您的应用程序,如果您强制终止应用程序,则出现“红线”是正常的。
-
是的,我强行终止了应用程序并显示了这个红线错误,但我没有在终止应用程序时获得位置更新。这里有什么问题??
-
@Cade..此代码不起作用,我正在努力寻找解决方案!!!
-
很高兴知道哪一行...以及错误...
标签: ios swift location core-location