为什么在 iOS 9.3 中的 kill/Terminate 应用程序后后台位置不起作用
Why background location is not working after kill/Terminate app in iOS 9.3
杀死/终止应用程序后后台位置不工作。我在我的应用程序中使用 NSLocationAlwaysUsageDescription 和 Background fetch 也来自 Capabilities,这里我的代码是:
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate{
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 1000.0
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedAlways){
let currentLocation = manager.location
let notification = UILocalNotification()
notification.alertBody = " Lat \(currentLocation?.coordinate.latitude), Long \(currentLocation?.coordinate.longitude)"
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
print(" Lat \((currentLocation?.coordinate.latitude))!, Long \((currentLocation?.coordinate.longitude))!")
}
}
}
此代码在单击主页按钮时有效,但在 terminate/kill 应用程序在这种情况下无法正常工作后,请在此处提供我在代码中遗漏的解决方案。
经过一些更改后,我通过执行此更改找到了自己的解决方案,并且代码在后台模式和前台模式下工作正常,现在可以获取位置。
Swift 3
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 2000
if #available(iOS 9.0, *) {
locationManager.allowsBackgroundLocationUpdates = true
}
locationManager.startMonitoringSignificantLocationChanges()
locationManager.pausesLocationUpdatesAutomatically = true
locationManager.requestAlwaysAuthorization()
杀死/终止应用程序后后台位置不工作。我在我的应用程序中使用 NSLocationAlwaysUsageDescription 和 Background fetch 也来自 Capabilities,这里我的代码是:
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate{
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 1000.0
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedAlways){
let currentLocation = manager.location
let notification = UILocalNotification()
notification.alertBody = " Lat \(currentLocation?.coordinate.latitude), Long \(currentLocation?.coordinate.longitude)"
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
print(" Lat \((currentLocation?.coordinate.latitude))!, Long \((currentLocation?.coordinate.longitude))!")
}
}
}
此代码在单击主页按钮时有效,但在 terminate/kill 应用程序在这种情况下无法正常工作后,请在此处提供我在代码中遗漏的解决方案。
经过一些更改后,我通过执行此更改找到了自己的解决方案,并且代码在后台模式和前台模式下工作正常,现在可以获取位置。
Swift 3
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 2000
if #available(iOS 9.0, *) {
locationManager.allowsBackgroundLocationUpdates = true
}
locationManager.startMonitoringSignificantLocationChanges()
locationManager.pausesLocationUpdatesAutomatically = true
locationManager.requestAlwaysAuthorization()