2
通過AppDelegate.swift中的代碼,我可以每隔30秒獲取一次位置更新。但是,180秒後後臺執行由backgroundTimeRemaining啓動。
plist中有UIBackgroundModes
「位置」的標籤我有兩個問題:更新後臺位置每n分鐘永遠在Swift 2.0中
- 爲什麼backgroundTimeRemaining未重置?
- 如果我執行startUpdatingLocation(),然後stopUpdatingLocation()來節省電池電量,那麼我將不會再獲取位置更新。
import UIKit
import CoreLocation
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var window: UIWindow?
let locationManager = CLLocationManager()
var pos = 0
var LatitudeGPS = NSString()
var LongitudeGPS = NSString()
var speedGPS = NSString()
var Course = NSString()
var Altitude = NSString()
var bgtimer = NSTimer()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}
var backgroundUpdateTask: UIBackgroundTaskIdentifier!
func beginBackgroundUpdateTask() {
self.backgroundUpdateTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({
self.endBackgroundUpdateTask()
})
}
func endBackgroundUpdateTask() {
UIApplication.sharedApplication().endBackgroundTask(self.backgroundUpdateTask)
self.backgroundUpdateTask = UIBackgroundTaskInvalid
}
func doBackgroundTask() {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
self.beginBackgroundUpdateTask()
// Do something
self.StartupdateLocation()
self.bgtimer = NSTimer.scheduledTimerWithTimeInterval(30, target: self, selector: "bgtimer:", userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(self.bgtimer, forMode: NSDefaultRunLoopMode)
NSRunLoop.currentRunLoop().run()
// End the background task.
self.endBackgroundUpdateTask()
})
}
func bgtimer(timer:NSTimer!){
print("Fired from Background ************************************")
updateLocation()
print("Position Report: \(pos)")
}
func applicationDidEnterBackground(application: UIApplication) {
self.doBackgroundTask()
}
func StartupdateLocation() {
locationManager.delegate = self
locationManager.startUpdatingLocation()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.requestWhenInUseAuthorization()
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false
}
func updateLocation() {
pos++
//locationManager.startUpdatingLocation()
//locationManager.stopUpdatingLocation()
print("Latitude: \(LatitudeGPS)")
print("Longitude: \(LongitudeGPS)")
print("Speed: \(speedGPS)")
print("Heading: \(Course)")
print("Altitude BG: \(Altitude)")
print(UIApplication.sharedApplication().backgroundTimeRemaining)
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
LatitudeGPS = String(format: "%.10f", manager.location!.coordinate.latitude)
LongitudeGPS = String(format: "%.10f", manager.location!.coordinate.longitude)
speedGPS = String(format: "%.3f", manager.location!.speed)
Altitude = String(format: "%.3f", manager.location!.altitude)
Course = String(format: "%.3f", manager.location!.course)
}
}
編輯*** 我解決了這個問題後,我初始化的視圖控制器的更新位置。
如果你解決了一個問題,然後做出答案。其他人(像我)會覺得它很有用:D – Fogmeister