即使在应用程序被终止/终止/暂停时,我也试图在所有状态下获取位置更新。我在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()
}
}
答案 0 :(得分:0)
这里的源代码: https://github.com/voyage11/GettingLocationWhenSuspended
希望你能得到答案。它为我工作。现在我正在尝试在' didUpdateLocations'函数被调用,以便我可以在应用程序未运行时将用户当前位置存储在服务器中(暂停/终止/终止)。
我不认为苹果允许在应用暂停/终止时发出任何http请求。但是如果服务器收到了位置更新的请求,那么我很高兴去那。因为我不需要从服务器回复。需要大量的测试....