我想检查该位置是否处于打开状态,如果该位置未打开,或者用户未授予使用位置的权限,则应退出(不会运行)。 有没有办法做到这一点,因为人们都说不推荐使用exit(0),这会让苹果难过。 :Dswift 2如果位置关闭,则阻止程序的运行
0
A
回答
2
您可以在屏幕上放置一个视图(全宽和高度),并带有一个标签,告诉用户只有在启用位置服务的情况下才能使用该应用。当然用户不应该以任何方式与这个视图进行交互。
这里有一个例子辅助类:
import UIKit
import CoreLocation
class LocationHelper: NSObject, CLLocationManagerDelegate {
private static let sharedInstance = LocationHelper()
private var locationManager: CLLocationManager! {
didSet {
locationManager.delegate = self
}
}
private override init() {}
class func setup() {
sharedInstance.locationManager = CLLocationManager()
}
private func informUserToEnableLocationServices() {
let infoPopup = UIAlertController(title: "Location Services", message: "Sorry, but you have to enable location services to use the app...", preferredStyle: .Alert)
let tryAgainAction = UIAlertAction(title: "Try again", style: .Default) { (action) in
if CLLocationManager.authorizationStatus() != .AuthorizedWhenInUse {
self.informUserToEnableLocationServices()
}
}
infoPopup.addAction(tryAgainAction)
let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
let rootViewController = appDelegate?.window?.rootViewController
rootViewController?.presentViewController(infoPopup, animated: true, completion: nil)
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .NotDetermined:
locationManager.requestWhenInUseAuthorization()
case .AuthorizedWhenInUse:
break
default:
informUserToEnableLocationServices()
}
}
}
只需拨打LocationHelper.setup()
推出了应用程序之后和类应处理剩下的...
1
苹果不喜欢exit(0)
的一个原因。我强烈建议您不要自行终止应用程序。也许你可以让用户使用功能有限的应用程序?另一种选择是在没有任何行动的情况下做出警戒,或者做出什么都不做的行动。
相关问题
- 1. 我的程序阻止Windows关闭!
- 2. 如果Internet Explorer自动关闭弹出窗口阻止程序
- 3. 阻止应用程序关闭
- 4. 如何阻止程序关闭时文件不存在/无法运行
- 5. 当控制台窗口关闭时阻止程序关闭
- 6. WiX:阻止32位安装程序在64位Windows上运行
- 7. 如何阻止fancyBox关闭?
- 8. 如果已经运行,阻止应用程序启动
- 9. PhoneGap的地理位置:轨道位置,如果应用程序关闭
- 10. 运行过程中,如果应用程序关闭
- 11. 当我从保存位置Python版本3.6.0运行Python程序时,如何停止关闭Python程序?
- 12. 如何阻止来电关闭我的程序?
- 13. 防止QApplication应用程序从关闭,如果服务正在运行
- 14. 如何阻止线程无限期地在Swift中运行?
- 15. 如何关闭/停止在后台运行应用程序android
- 16. 如何在关闭服务器套接字时阻止应用程序关闭。
- 17. 阻止来自Ruby应用程序的关闭进程
- 18. 如果它打开或关闭,则关闭div如果关闭
- 19. 如何阻止C#控制台应用程序自动关闭?
- 20. 如何阻止强制关闭应用程序?
- 21. 如何阻止JavaScript运行的线程?
- 22. BAT关闭程序,如果另一个正在运行
- 23. 批处理:如果程序没有运行,关闭PC?
- 24. 应用程序强行关闭运行。
- 25. 阻止应用关闭
- 26. 阻止Android应用。关闭
- 27. Android阻止键盘关闭
- 28. 阻止用户关闭Firefox
- 29. 线程未正确完成,阻止应用程序关闭
- 30. 如果一个程序已经在运行,我该如何关闭程序?
[退出iPhone应用程序的正确方法](http://stackoverflow.com/q/355168/335858) – dasblinkenlight