我一直在閱讀蘋果開發者的資源,瞭解位置更新如何工作以及讀取堆棧溢出問題。我試圖讓我的用戶在我的應用程序的位置,所有的時候,用戶把應用程序在後臺。下面的代碼不是我的代碼,我發現它來自另一個堆棧溢出源問題,它是唯一讓我接近我需要的東西。下面的代碼工作和更新我的用戶位置只有25分鐘,然後只是停止,但我想要的是它始終工作,只要該應用程序在後臺:(。這個問題已答覆,但我'我聽說蘋果會在3分鐘左右自動終止你的應用程序,所以無論如何繞過這將有助於。我也有我的背景模式檢查,如位置更新和背景獲取甚至還需要info.plist,這是所說的假設自動喚醒我的應用程序,一旦終止,但似乎也沒有工作,如果任何人都可以幫助我這將是一個巨大的幫助,我的新應用程序,這是我需要aha的代碼的最後一點,預先感謝大家。還想做到這一點,沒有添加一個無聲的音頻文件,所以我的應用程序不會被拒絕:)在後臺連續快速運行應用程序
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.allowsBackgroundLocationUpdates = true
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
var timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "updateLocation", userInfo: nil, repeats: true)
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
if UIApplication.sharedApplication().applicationState == .Active {
} else {
backgroundTaskIdentifier = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({() -> Void in
self.backgroundTimer.invalidate()
self.backgroundTimer = NSTimer.scheduledTimerWithTimeInterval(60.0, target: self, selector: "updateLocation", userInfo: nil, repeats: true)
})
}
}
func updateLocation() {
var timeRemaining = UIApplication.sharedApplication().backgroundTimeRemaining
print(timeRemaining)
if timeRemaining > 60.0 {
print("timeRemaining > 60.0")
}
} else {
if timeRemaining == 0 {
print("timeRemaining = 0") UIApplication.sharedApplication().endBackgroundTask(backgroundTaskIdentifier)
}
backgroundTaskIdentifier2 = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({() -> Void in
self.backgroundTimer.invalidate()
self.backgroundTimer = NSTimer.scheduledTimerWithTimeInterval(60.0, target: self, selector: "updateLocation", userInfo: nil, repeats: true)
})
}
}
你爲什麼要這樣做?無論何時移動500.0米,您都可以更新用戶的位置。除非用戶移動距離,否則他的位置將保持不變。 – Rikh
我的應用程序需要知道他們在任何時候的應用程序的主要目的:) –