How do I get a background location update every n minutes in my iOS application?

后端 未结 14 1171
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 02:00

I\'m looking for a way to get a background location update every n minutes in my iOS application. I\'m using iOS 4.3 and the solution should work for non-jailbroken iPhones

14条回答
  •  死守一世寂寞
    2020-11-22 02:07

    Attached is a Swift solution based in:

    Define App registers for location updates in the info.plist

    Keep the locationManager running all the time

    Switch kCLLocationAccuracy between BestForNavigation (for 5 secs to get the location) and ThreeKilometers for the rest of the wait period to avoid battery drainage

    This example updates location every 1 min in Foreground and every 15 mins in Background.

    The example works fine with Xcode 6 Beta 6, running in a iOS 7 device.

    In the App Delegate (mapView is an Optional pointing to the mapView Controller)

    func applicationDidBecomeActive(application: UIApplication!) {
        if appLaunched! == false { // Reference to mapView used to limit one location update per timer cycle
            appLaunched = true
            var appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
            var window = appDelegate.window
            var tabBar = window?.rootViewController as UITabBarController
            var navCon = tabBar.viewControllers[0] as UINavigationController
            mapView = navCon.topViewController as? MapViewController
        }
        self.startInitialPeriodWithTimeInterval(60.0)
    }
    
    func applicationDidEnterBackground(application: UIApplication!) {
        self.startInitialPeriodWithTimeInterval(15 * 60.0)
    }
    
    func startInitialPeriodWithTimeInterval(timeInterval: NSTimeInterval) {
        timer?.invalidate() // reset timer
        locationManager?.desiredAccuracy = kCLLocationAccuracyBestForNavigation
        timer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: Selector("getFirstLocationUpdate:"), userInfo: timeInterval, repeats: false)
    }
    
    func getFirstLocationUpdate(sender: NSTimer) {
        let timeInterval = sender.userInfo as Double
        timer?.invalidate()
        mapView?.canReportLocation = true
        timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval, target: self, selector: Selector("waitForTimer:"), userInfo: timeInterval, repeats: true)
    }
    
    func waitForTimer(sender: NSTimer) {
        let time = sender.userInfo as Double
        locationManager?.desiredAccuracy = kCLLocationAccuracyBestForNavigation
        finalTimer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: Selector("getLocationUpdate"), userInfo: nil, repeats: false)
    }
    
    func getLocationUpdate() {
        finalTimer?.invalidate()
        mapView?.canReportLocation = true
    }
    

    In the mapView (locationManager points to the object in the AppDelegate)

    override func viewDidLoad() {
        super.viewDidLoad()
        var appDelegate = UIApplication.sharedApplication().delegate! as AppDelegate
        locationManager = appDelegate.locationManager!
        locationManager.delegate = self
        canReportLocation = true
    }
    
      func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
            if canReportLocation! {
                canReportLocation = false
                locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
            } else {
                //println("Ignore location update")
            }
        }
    

提交回复
热议问题