2015-12-30 56 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請求,當應用程序被暫停/終止。但是,如果服務器收到位置更新的請求,那麼我很樂意去做。因爲我不需要從服務器迴應。需要大量的測試....

相關問題