我已经在后台模式功能下启用了位置服务,如果单击主页按钮,一切正常,我会得到更新。我甚至在使用safari时获得更新,直到我去Facebook或其他什么,它只是停止。该应用程序没有崩溃或任何东西,它不会消耗太多的内存,也不会消耗大量的电力。我的应用程序有一个NSTimer,每隔30秒才需要该位置。
App Delegate:
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 {
manager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
manager.delegate = self
manager.requestAlwaysAuthorization()
manager.startUpdatingLocation()
return true
}
主视图控制器:
import UIKit
import CoreLocation
class MainViewController: UIViewController, SettingsViewDelegate {
@IBOutlet weak var pageMenuView: UIView!
var audioPlayer = AVAudioPlayer()
var lastTimestamp: NSDate!
var timer : NSTimer?
lazy var locationManager: CLLocationManager = {
var _locationManager = CLLocationManager()
_locationManager.delegate = self
_locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
// Movement threshold for new events
return _locationManager
}()
override func viewDidLoad() {
super.viewDidLoad()
self.timer = NSTimer.scheduledTimerWithTimeInterval(30, target: self, selector: #selector(MainViewController.startLocationUpdates), userInfo: nil, repeats: true)
}
func startLocationUpdates() {
// Here, the location manager will be lazily instantiated
locationManager.startUpdatingLocation()
}
}
// MARK: - CLLocationManagerDelegate
extension MainViewController: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("location \(reminderTime)")
self.locationManager.stopUpdatingLocation()
}
}
更新 此问题发生在物理设备上,iphone 5c