简体   繁体   English

应用程序位置在iOS 8的后台

[英]App location in background on iOS 8

I want get a user's location in app background. 我想在应用程序背景中获取用户的位置。 It works fine if I use a timer of 9 or less seconds. 如果我使用9秒或更短秒的计时器,它工作正常。 If I use 10 and more seconds I can't get the user's location... 如果我使用10秒甚至更长时间,我无法获得用户的位置...

My AppDelegate.swift : 我的AppDelegate.swift

import UIKit
import CoreLocation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
    var window: UIWindow?
    var backgroundTaskIdentifier: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid
    var myTimer: NSTimer?

    var locationManager = CLLocationManager()

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func isMultitaskingSupported() -> Bool {
        return UIDevice.currentDevice().multitaskingSupported
    }

    func timerMethod(sender: NSTimer) {
        let backgroundTimerRemainig = UIApplication.sharedApplication().backgroundTimeRemaining

        self.locationManager.startUpdatingLocation()

        if backgroundTimerRemainig == DBL_MAX {
            println("test1")
        } else {
            println("test")
            self.locationManager.delegate = self
            self.locationManager.startUpdatingLocation()
        }
    }

    func applicationWillResignActive(application: UIApplication) {

    }

    func applicationDidEnterBackground(application: UIApplication){
        if !isMultitaskingSupported() {
            return
        }

        myTimer = NSTimer.scheduledTimerWithTimeInterval(60.0,
            target: self,
            selector: "timerMethod:",
            userInfo: nil,
            repeats: true)

        backgroundTaskIdentifier = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler {
            () -> Void in
            UIApplication.sharedApplication().endBackgroundTask(self.backgroundTaskIdentifier)
        }
    }

    func endBackgroundTask() {
        let mainQueue = dispatch_get_main_queue()

        dispatch_async(mainQueue) { 
            [weak self] in
            if let timer = self!.myTimer {
                timer.invalidate()
                self!.myTimer = nil
                UIApplication.sharedApplication().endBackgroundTask(self!.backgroundTaskIdentifier)
                self!.backgroundTaskIdentifier = UIBackgroundTaskInvalid
            }
        }
    }

    func applicationWillEnterForeground(application: UIApplication) {
        if backgroundTaskIdentifier != UIBackgroundTaskInvalid {
            endBackgroundTask()
        }
    }

    func applicationDidBecomeActive(application: UIApplication) {

    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

    func locationManager(manager: CLLocationManager!, didUpdateToLocation locations: [AnyObject]) {
        var latValue = locationManager.location.coordinate.latitude
        var lonValue = locationManager.location.coordinate.longitude
    }
}

Why can't I get the user's location when I use 10 and more seconds? 当我使用10秒甚至更长时间时,为什么我无法获得用户的位置?

Because approximately after 10 seconds after calling didEnterBackground the app gets suspended . 因为大约在调用didEnterBackground 10秒后应用程序被暂停

The app is in the background but is not executing code. 该应用程序在后台,但没有执行代码。 The system moves apps to this state automatically and does not notify them before doing so. 系统会自动将应用程序移动到此状态,并且在执行此操作之前不会通知它们。 While suspended, an app remains in memory but does not execute any code. 暂停时,应用程序仍保留在内存中,但不执行任何代码。

It's the iOS app lifecycle . 这是iOS应用程序生命周期

If you want to get location updates in background you have two options: 如果您想在后台获取位置更新,您有两种选择:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM