简体   繁体   English

Swift 3.0允许权限警报不停止

[英]Swift 3.0 Allow Permission Alert Non-Stop

I want to get user location use CCLocationManager. 我想使用CCLocationManager获取用户位置。 I could get location clearly but "Allow location permission" alert showing repeatedly and non-stop when start first time on any device. 我可以清楚地获得位置信息,但是在任何设备上首次启动时都会反复显示“允许位置信息允许”警报,并且不停止。

What should I do? 我该怎么办?

My AppDelegate.swift code; 我的AppDelegate.swift代码;

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
    }
}

Alert Image: 警报图片:

在此处输入图片说明

而是将您的位置权限代码放入didFinishLaunchingWithOptions ,因此将被询问一次,然后在NSDefault设置任何布尔值,例如alreadyAskedPermission变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM