如果应用程序在IOS 9中暂停/关闭,则获取位置

时间:2015-12-18 14:14:10

标签: ios location swift2 cllocationmanager

iOS使用:9

我正在尝试在应用暂停时检索位置更新。如果应用程序处于后台或前台模式,我知道如何执行此操作,但如果它被暂停,如何在iOS 9中完成?目前我尝试将我的位置记录到文本文件但没有记录任何内容。 这是我的代码。当然我启用了位置更新非后台模式并编辑了我的plist文件。

在didFinishWithLaunchOptions

locationManager.requestAlwaysAuthorization()

    if let launchOpts = launchOptions
    {

    if ((launchOpts[UIApplicationLaunchOptionsLocationKey]) != nil)
    {
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.startMonitoringSignificantLocationChanges()
    }
    }

func foundLocation(location: CLLocation!)
    {

        let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])
        let logsPath = documentsPath.URLByAppendingPathComponent(NSLocalizedString("FolderName", comment: ""))

        let filePath = logsPath.URLByAppendingPathComponent(String("textfile.txt"));

        if let outputStream = NSOutputStream(toFileAtPath: filePath.path!, append: true) {
            outputStream.open()
                outputStream.write("/***/ Text: \(location)\n\r")

            outputStream.close()
        } else {
        }
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        foundLocation(manager.location) // update the user object in this method

    }

0 个答案:

没有答案