iOS后台模式位置更新 - 经理在后台模式下无法更新

时间:2015-10-28 07:55:40

标签: ios swift2

我刚刚关注http://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial位置更新部分。

但是经理不以后台模式打印位置信息。

然后当app进入前台时,经理打印日志到Xcode的控制台。

这段代码是对的吗?

<!DOCTYPE composition PUBLIC ..bla...XHTML 1.0 Transitional..bla...>

1 个答案:

答案 0 :(得分:9)

我自己得到了答案。

如果您的应用在iOS 9.0或更高版本上运行,并希望在后台运行您的位置服务应用

你必须将allowsBackgroundLocationUpdates变量设置为true。

所以这是我的代码。

import UIKit
import CoreLocation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

var window: UIWindow?
var manager = CLLocationManager()


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

    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.delegate = self
    manager.requestAlwaysAuthorization()
    manager.startUpdatingLocation()
    if #available(iOS 9.0, *){
        manager.allowsBackgroundLocationUpdates = true
    }   

    return true
}

func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
    if UIApplication.sharedApplication().applicationState != .Active {
        NSLog("App is backgrounded. New location is %@", newLocation)
    }
}
.....

}