作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要在 applicationDidEnterBackground 时停止来自 AppDelegate 的 locationUpdates,并在来自 ViewController 的 applicationDidBecomeActive 时停止 startUpdatingLocation,这是我的代码..
如果我的 locationManager 在 ViewController 中,该怎么做。
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate
var seenError : Bool = false
var locationFixAchieved : Bool = false
var locationStatus : NSString = "Not Started"
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
}
func initLocationManager() {
seenError = false
locationFixAchieved = false
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.locationServicesEnabled
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
locationManager.stopUpdatingLocation()
if (error) {
if (seenError == false) {
seenError = true
print(error)
}
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!) {
if (locationFixAchieved == false) {
locationFixAchieved = true
var locationArray = locations as NSArray
var locationObj = locationArray.lastObject as CLLocation
var coord = locationObj.coordinate
println(coord.latitude)
println(coord.longitude)
}
}
func locationManager(manager: CLLocationManager!,
didChangeAuthorizationStatus status: CLAuthorizationStatus) {
var shouldIAllow = false
switch status {
case CLAuthorizationStatus.Restricted:
locationStatus = "Restricted Access to location"
case CLAuthorizationStatus.Denied:
locationStatus = "User denied access to location"
case CLAuthorizationStatus.NotDetermined:
locationStatus = "Status not determined"
default:
locationStatus = "Allowed to location Access"
shouldIAllow = true
}
NSNotificationCenter.defaultCenter().postNotificationName("LabelHasbeenUpdated", object: nil)
if (shouldIAllow == true) {
NSLog("Location to Allowed")
// Start location services
locationManager.startUpdatingLocation()
} else {
NSLog("Denied access: \(locationStatus)")
}
}
最佳答案
NSNotificationCenter
是一种在您的应用程序中发送消息的简单方法。每个需要响应此类消息的类都注册一个观察者来监听消息。
AppDelegate:
func applicationDidEnterBackground(application: UIApplication) {
// Send a message that informs all listening classes for entering background
NSNotificationCenter.defaultCenter().postNotification(NSNotification(name: "appEntersBackground", object: nil))
}
func applicationDidBecomeActive(application: UIApplication) {
// Send a message that informs all listening classes for becoming active again
NSNotificationCenter.defaultCenter().postNotification(NSNotification(name: "appBecomesActive", object: nil))
}
swift 4.2
NotificationCenter.default.post(name: Notification.Name("appEntersBackground"), object: nil)
View Controller
在您的 viewController 中,为消息注册观察者:
class ViewController: UIViewController, CLLocationManagerDelegate {
...
override func viewDidLoad() {
super.viewDidLoad()
// register observers
NSNotificationCenter.defaultCenter().addObserver(self, selector: "enterBackground", name: "appEntersBackground", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "becomeActive", name: "appBecomesActive", object: nil)
}
deinit {
// remove observers
NSNotificationCenter.defaultCenter().removeObserver(self)
}
// app enters background
func enterBackground() {
self.locationManager.stopUpdatingLocation()
}
// app becomes active
func becomeActive() {
self.locationManager.startUpdatingLocation()
}
}
swift 4.2
NotificationCenter.default.addObserver(self, selector: #selector(enterBackground), name: Notification.Name("appEntersBackground"), object: nil)
@objc func enterBackground() {
self.locationManager.stopUpdatingLocation()
}
请注意,locationmanager 会停止在后台更新位置,除非您已在 Info.plist
中为后台任务注册您的应用。
关于ios - 如何在 applicationDidEnterBackground 中停止来自 ViewController 的 locationManagerUpdates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29339408/
我需要在 applicationDidEnterBackground 时停止来自 AppDelegate 的 locationUpdates,并在来自 ViewController 的 applica
我是一名优秀的程序员,十分优秀!