Tôi đã tạo một hàm giả sử để xử lý quyền với vị trí, để ứng dụng sẽ đóng nếu ứng dụng không có quyền truy cập vị trí. Tuy nhiên, khi bạn bấm thiết lập mở và bấm "quay lại ứng dụng" trên thanh trạng thái, phương thức determeinePermission
sẽ không được thực hiện lại. Tôi đã cố thêm nó vào viewDidLoad
, viewDidAppear
và viewWillAppear
. tôi có thể làm gì?Phát hiện khi "quay lại ứng dụng" được nhấn
func determinePermission() {
switch CLLocationManager.authorizationStatus() {
case .Authorized:
if CLLocationManager.locationServicesEnabled() {
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
manager.startUpdatingLocation()
}
case .NotDetermined:
manager.requestWhenInUseAuthorization()
case .AuthorizedWhenInUse, .Restricted, .Denied:
let alertController = UIAlertController(
title: "Background Location Access Disabled",
message: "In order to be notified about adorable kittens near you, please open this app's settings and set location access to 'Always'.",
preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
exit(0)
}
alertController.addAction(cancelAction)
let openAction = UIAlertAction(title: "Open Settings", style: .Default) { (action) in
if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
UIApplication.sharedApplication().openURL(url)
}
}
alertController.addAction(openAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
}
Thử thêm nó vào appDidBecameActive. P.S. bạn không thể đóng ứng dụng theo chương trình mà bạn muốn làm. Không cho phép thoát (0) trong mã của bạn. – Gruntcakes
cũng ổn, tôi sẽ tìm thấy một giải pháp cho điều đó :) nhưng vấn đề đang gọi một phương pháp khi nhấn trở lại –
Bạn đã thử với appDidBecomeActive chưa? – Gruntcakes