Ios Swift 3.0允许权限警报不停止
我想使用CCLocationManager获取用户位置。我可以清楚地获取位置,但允许位置权限警报在任何设备上第一次启动时重复显示且不停止 我该怎么办 我的AppDelegate.swift代码Ios Swift 3.0允许权限警报不停止,ios,swift,cllocationmanager,Ios,Swift,Cllocationmanager,我想使用CCLocationManager获取用户位置。我可以清楚地获取位置,但允许位置权限警报在任何设备上第一次启动时重复显示且不停止 我该怎么办 我的AppDelegate.swift代码 class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate { var window: UI
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var window: UIWindow?
func applicationDidBecomeActive(_ application: UIApplication) {
let locationManager = CLLocationManager()
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
if ((locationManager.location?.coordinate) != nil) {
let locValue:CLLocationCoordinate2D = (locationManager.location?.coordinate)!
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: locValue.latitude, longitude: locValue.longitude)
geoCoder.reverseGeocodeLocation(location) {
(placemarks, error) -> Void in
let placeArray = placemarks as [CLPlacemark]!
// Place details
var placeMark: CLPlacemark!
placeMark = placeArray?[0]
if let state = placeMark.addressDictionary?["State"] as? NSString {
let parameters: Parameters = ["Location": state]
Alamofire.request(GlobalValues.APIUrl, method: .post, parameters: parameters).validate().responseJSON{
response in
if let result = response.result.value {
let JSON = result as! NSDictionary
let items = JSON.object(forKey: "Items") as! NSDictionary
userData.set(items.object(forKey: "fajr") as! String, forKey: "sabahNamazi")
userData.set(items.object(forKey: "dhuhr") as! String, forKey: "ogleNamazi")
userData.set(items.object(forKey: "asr") as! String, forKey: "ikindiNamazi")
userData.set(items.object(forKey: "maghrib") as! String, forKey: "aksamNamazi")
userData.set(items.object(forKey: "isha") as! String, forKey: "yatsiNamazi")
}
}
}
}
} else {
// create the alert
let alert = UIAlertController(title: "Konum Alınamıyor", message: "Telefonunuzun konum ayarlarını kontrol edip tekrar deneyin!", preferredStyle: UIAlertControllerStyle.alert)
// add the actions (buttons)
alert.addAction(UIAlertAction(title: "Tamam", style: UIAlertActionStyle.default, handler: nil))
// show the alert
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
} else {
// create the alert
let alert = UIAlertController(title: "Konum Ayarını Açın", message: "Bu uygulama bulunduğunuz şehrin 'EZAN VAKİTLERİNİ' tespit edebilmek için konum bilginize ihtiyaç duyuyor.", preferredStyle: UIAlertControllerStyle.alert)
// add the actions (buttons)
alert.addAction(UIAlertAction(title: "Ayarlar", style: UIAlertActionStyle.default, handler: {(
action:UIAlertAction!) -> Void in
UIApplication.shared.openURL(URL(string: "prefs:root=LOCATION_SERVICES")!)
}))
alert.addAction(UIAlertAction(title: "İptal", style: UIAlertActionStyle.cancel, handler: nil))
// show the alert
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
}
警报图像:
将您的位置权限代码改为didFinishLaunchingWithOptions,这样它将被询问一次,然后在NSDefault中设置任何布尔值,如alreadyAskedPermission变量,只使用一个请求。。选择locationManager.requestAlwaysAuthorization或locationManager.RequestWhenUseAuthorization您好,当我只使用locationManager.requestAlwaysAuthorization或locationManager.RequestWhenUseAuthorization时,我得到了相同的结果。。将您的代码从ApplicationIDBecomeActive移动。。每次应用激活时,您都将创建一个新的CLLocationManager实例。。。将代码移动到didFinishLaunchingWithOptions..@CelilBozkurt:True,但这可能会导致权限对话框出现两次。