您好我正在使用CLLocation到我的应用程序,我已经初始化了我的CLLocationManager:
func initLocationManager(){
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
let authstate = CLLocationManager.authorizationStatus()
if(authstate == CLAuthorizationStatus.NotDetermined || authstate == CLAuthorizationStatus.Denied){
println("Not Authorised")
locationManager.requestWhenInUseAuthorization()
}
locationManager.startUpdatingLocation()
}
我还在我的plist中添加了NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription键。
第一次打开我的应用程序时,我收到提示消息,我的应用程序想要访问位置,它有2个按钮允许,不允许。如果我点击不允许按钮并关闭应用程序,当我再次打开它时,我再也没有得到提示消息。
每次用户打开应用时,如何显示此提示消息?谢谢
答案 0 :(得分:3)
首先执行代码提示警报,并在位置服务 已禁用
时执行自定义提醒import UIKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
initLocationManager()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func initLocationManager(){
let status = CLLocationManager.authorizationStatus()
if(status == CLAuthorizationStatus.NotDetermined) {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
let iosVersion = NSString(string: UIDevice.currentDevice().systemVersion).doubleValue
if iosVersion >= 8.0 {
//For Foreground
locationManager.requestWhenInUseAuthorization()
}
locationManager.startUpdatingLocation()
} else {
if(status != CLAuthorizationStatus.AuthorizedWhenInUse) {
var alert = UIAlertView(title: "Location", message: "Please turn on Location Services", delegate: nil, cancelButtonTitle: "Cancel")
alert.addButtonWithTitle("Open Setting")
alert.show()
/*Add Action on Open Setting alertbutton to directly open settings in iOS 8 and later
-> UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)*/
}
}
}
}
答案 1 :(得分:2)
一旦用户拒绝,就无法执行此操作,您必须显示一个对话框,向用户说明他/她必须转到“设置”并手动允许该功能。
答案 2 :(得分:0)
此消息要求获得使用设备GPS的权限,并尝试获取设备的位置,这就是需要GPS的原因。第二个你不想显示它你可以使用NSUseDefaults创建条件并存储密钥然后不要调用方法locationmanager startupdatinglocation。这是不再显示的唯一方法,否则每次都会显示。