2016-10-17 18 views
2

CCLocationManagerを使用してユーザーの場所を取得したい。私は場所をはっきりと知ることができましたが、どの場所でも最初に起動すると「場所の許可を許可する」警告が繰り返し表示され、ノンストップで表示されます。Swift 3.0許可許可アラートノンストップ

どうすればよいですか?

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

アラートイメージ:

enter image description here

+1

使用中なalreadyAskedPermission変数として任意のブール値を設定されます。 「1つのリクエスト」だけが 'locationManager.requestAlwaysAuthorization'または' locationManager.requestWhenInUseAuthorization'を選択しました – TonyMkenu

+0

こんにちは、私は 'locationMan ager.requestAlwaysAuthorization'または 'locationManager.requestWhenInUseAuthorization' –

+0

まず、' applicationDidBecomeActive'からあなたのコードを移動します。あなたのアプリケーションがアクティブになるたびに 'CLLocationManager'の新しいインスタンスを作成します。コードを' didFinishLaunchingWithOptions'に移動します。 – TonyMkenu

答えて

4

ではなくdidFinishLaunchingWithOptionsであなたの場所の許可コードを置くので、それは、一度尋ねた後、NSDefault