2015-12-30 59 views
5

即使在应用程序被终止/终止/挂起时,我仍然试图获得位置更新,即使在所有状态下也是如此。我已经在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() 
    } 


} 
+1

可能重复:http://stackoverflow.com/questions/30396367/getting-location-for-an-ios-app-when-it-is-in-the-background甚至杀了 –

+1

*“它给AppDelegate类上的红线”* - 这是最糟糕和最不有用的错误描述。请在这里发布错误消息。如果您通过Xcode运行应用程序,如果您强制终止应用程序,则显示“红线”是正常的。 – luk2302

+0

是的,我强制终止应用程序,并显示此红线错误,但我没有得到终止应用程序的位置更新。这里的问题是什么? – Kirti

回答

0

检查本教程: http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended

和源代码在这里: https://github.com/voyage11/GettingLocationWhenSuspended

希望你能得到你的答案。它为我工作。现在我试图在'didUpdateLocations'函数被调用时发出http请求,以便在应用程序未运行时(暂停/终止/终止),我可以将用户当前位置存储在服务器中。

我不认为苹果允许任何HTTP请求,当应用程序被暂停/终止。但是,如果服务器收到位置更新的请求,那么我很乐意去做。因为我不需要从服务器回应。需要大量的测试....

相关问题