![](/img/trans.png)
[英]How to access applicationDidEnterBackground from viewController
[英]How stop locationManagerUpdates from ViewController in applicationDidEnterBackground
我需要在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))
}
斯威夫特4.2
NotificationCenter.default.post(name: Notification.Name("appEntersBackground"), object: nil)
視圖控制器
在您的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()
}
}
斯威夫特4.2
NotificationCenter.default.addObserver(self, selector: #selector(enterBackground), name: Notification.Name("appEntersBackground"), object: nil)
@objc func enterBackground() {
self.locationManager.stopUpdatingLocation()
}
請注意,除非您已在Info.plist
為后台任務注冊了應用程序,否則locationmanager會停止在后台更新位置。
您可以觸發通知startUpdatingLocation
從applicationDidBecomeActive
和stopUpdatingLocation
從applicationDidEnterBackground
。 然后,您可以在視圖控制器中偵聽這些通知以管理locationManager
。
您可以使用此代碼-
func applicationDidEnterBackground(application: UIApplication) {
self.locationManager.stopUpdatingLocation()
}
func applicationDidBecomeActive(application: UIApplication) {
self.locationManager.startUpdatingLocation()
}
還有一個建議,您可以在initLocationManager
方法中進行檢查-
if (self.locationManager.respondsToSelector(Selector("requestAlwaysAuthorization"))) {
self.locationManager.requestAlwaysAuthorization()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.