即使在應用程序被終止/終止/掛起時,我仍然試圖獲得位置更新,即使在所有狀態下也是如此。我已經在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