gpt4 book ai didi

ios - 如何在 applicationDidEnterBackground 中停止来自 ViewController 的 locationManagerUpdates

转载 作者:行者123 更新时间:2023-11-28 10:24:38 25 4
gpt4 key购买 nike

我需要在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com