首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >应用程序在iOS 8背景中的定位

应用程序在iOS 8背景中的定位
EN

Stack Overflow用户
提问于 2015-03-05 17:46:51
回答 1查看 2K关注 0票数 0

我想要一个用户的位置在应用程序背景。如果我使用9秒或更短的计时器,它就能正常工作。如果我用了10秒钟甚至更长的时间,我就无法得到用户的位置.

我的AppDelegate.swift

代码语言:javascript
复制
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
    }
}

为什么我不能得到用户的位置,当我使用10秒以上?

EN

回答 1

Stack Overflow用户

发布于 2015-03-05 18:14:26

因为在调用didEnterBackground大约10秒后,应用程序就会被挂起

该应用程序在后台,但没有执行代码。系统会自动将应用程序移动到此状态,并且在此之前不会通知它们。当应用程序挂起时,它会留在内存中,但不会执行任何代码。

iOS应用生命周期

如果您想获得背景中的位置更新,您有两个选项:

  • 背景任务 -但它只让应用程序最多运行3分钟
  • 运行更新位置后台模式
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28884343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档