2016-12-03 2 views
0

下記のトーマスのアドバイスのおかげで、元のリクエストからコードを修正しました。不可能なことをしようとしているのか、そうでないのか、私はこれをどうやって達成できるのか疑問に思います。アプリロケーション設定の変更後にアプリを実行する際にCLLocationManager.authorizationStatusを再評価してください

ユーザーが位置情報サービスを承認していない場合は、「設定を開く」ボタンを使用して警告を表示して、アプリの位置設定を変更します。これは機能します。しかし、設定からアプリに戻ったときに、変更が行われたかどうかを認識し、位置情報サービスを有効にしたいと思います。これはできますか?以下のクロージャは、ユーザーがアプリケーションの設定に成功し、ユーザーは変更を加えることができ、ユーザーは戻ることができますが、設定が変更される前にユーザーが「設定を開く」を押すとクロージャが実行されます。 BTW:アプリの現在の権限のない場所の設定を承認するためにユーザを微調整するより良い方法がある場合は、アドバイスをお願いします。ありがとう!

私はここに拡張コードは、私が欲しいものを実現考える
locationManager.requestWhenInUseAuthorization() // request authorization 

let authStatus = CLLocationManager.authorizationStatus() 

switch authStatus { 
    case .denied: 
     let alertController = UIAlertController(title: "Background Location Access Disabled", message: "In order to show the location weather forecast, please open this app's settings and set location access to 'While Using'.", preferredStyle: .alert) 
     alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) 
     alertController.addAction(UIAlertAction(title: "Open Settings", style: .`default`, handler: { action in 
      if #available(iOS 10.0, *) { 
       let settingsURL = URL(string: UIApplicationOpenSettingsURLString)! 
       UIApplication.shared.open(settingsURL, options: [:], completionHandler: {(success) in 
        print("*** Success closure fires") 
        let newAuthStatus = CLLocationManager.authorizationStatus() 
        switch newAuthStatus { 
        case .authorizedWhenInUse: 
         self.locationManager.startUpdatingLocation() 
        default: 
         print("Not .authorizedWhenInUse") 
        } 
       }) 
      } else { 
       if let url = NSURL(string:UIApplicationOpenSettingsURLString) { 
        UIApplication.shared.openURL(url as URL) 
       } 
      } 
     })) 
     self.present(alertController, animated: true, completion: nil) 

    case .authorizedWhenInUse, .authorizedAlways: 
     locationManager.startUpdatingLocation() 
    case .restricted : 
     print("App is restricted, likely via parental controls.") 
    default: 
     print("UH!! WHAT OTHER CASES ARE THERE? ") 
    } 
+0

私は何を誤解していることは 'present'方法だと思います。ビューコントローラーの表示が完了すると、完成が完了します。私はあなたの説明から少しはっきりしていませんが、ユーザーが警告オプションの1つを選択した後にこのアクションを開始するように思えます。したがって、このコードはUIAlertActionの完了ハンドラの1つになければなりません。 –

+0

ありがとう、トーマス。これは役に立ちました。私はクロージャーを動かすことができました。ユーザーが「設定を開く」ボタンをクリックしたときに実行されました。しかし、私は不可能に頼んでいるのだろうかと思っています。ユーザーがアプリの設定からアプリに戻ったときに、UIApplicationOpenSettingsURLStringを使ってもう一度認証をテストしたいと考えています。ユーザーが設定を変更して戻ってきたときに何らかの形でコードを実行することができますか?あなたのアドバイスをもう一度おねがいします – Gallaugher

答えて

1

- シンプルなGETの場所が、すべての認証ステータスの例を処理する: - .notDetermined:requestWhenInUseAuthorization - .authorized:startUpdatingLocations - .denied:プロンプトユーザーをUIApplicationOpenSettingsURLStringを使用してアプリのプライバシー/場所設定を開き、変更を行うことができるアラートが表示されます。新しいステータスのアプリへの戻り値はdidUpdateLocationsで取得され、設定を更新した後にユーザーの場所が取得されます。 - .restricted - 親またはシステム管理者にアプリの制限を解除するように警告する警告が表示されます。

警告はdidFailWithErrorの場合もエラーコードを表示します。

だけlocationManager & currentLocation

let locationManager = CLLocationManager() 
var currentLocation: CLLocation! 

のインスタンス変数を設定してのviewDidLoadで、デリゲート&コールのgetLocation()

locationManager.delegate =自己 のgetLocationを(設定)

うまくいけば、このそれはより良い方法でrecsが大歓迎です。私はスウィフト3にあったものを見つけるのに苦労したので、誰かを助けてくれることを願っています。&おかげさまでトーマス!

拡張子のViewController:CLLocationManagerDelegate {

func getLocation() { 

    let status = CLLocationManager.authorizationStatus() 

    handleLocationAuthorizationStatus(status: status) 
} 

func handleLocationAuthorizationStatus(status: CLAuthorizationStatus) { 
    switch status { 
    case .notDetermined: 
     locationManager.requestWhenInUseAuthorization() 
    case .authorizedWhenInUse, .authorizedAlways: 
     locationManager.startUpdatingLocation() 
    case .denied: 
     print("I'm sorry - I can't show location. User has not authorized it") 
     statusDeniedAlert() 
    case .restricted: 
     showAlert(title: "Access to Location Services is Restricted", message: "Parental Controls or a system administrator may be limiting your access to location services. Ask them to.") 
    } 
} 

func showAlert(title: String, message: String) { 
    let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) 

    let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil) 

    alertController.addAction(defaultAction) 

    present(alertController, animated: true, completion: nil) 
} 

func statusDeniedAlert() { 
    let alertController = UIAlertController(title: "Background Location Access Disabled", message: "In order to show the location weather forecast, please open this app's settings and set location access to 'While Using'.", preferredStyle: .alert) 
    alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) 
    alertController.addAction(UIAlertAction(title: "Open Settings", style: .`default`, handler: { action in 
     if #available(iOS 10.0, *) { 
      let settingsURL = URL(string: UIApplicationOpenSettingsURLString)! 
      UIApplication.shared.open(settingsURL, options: [:], completionHandler: nil) 
     } else { 
      if let url = NSURL(string:UIApplicationOpenSettingsURLString) { 
       UIApplication.shared.openURL(url as URL) 
      } 
     } 
    })) 
    self.present(alertController, animated: true, completion: nil) 
} 

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 
    handleLocationAuthorizationStatus(status: status) 
} 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    if let currentLocation = locations.last { 
     print("My coordinates are: \(currentLocation.coordinate.latitude), \(currentLocation.coordinate.longitude)") 
     locationManager.stopUpdatingLocation() 
     updateUserInterface() 
    } 
} 

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { 
    showAlert(title: "Location Access Failure", message: "App could not access locations. Loation services may be unavailable or are turned off. Error code: \(error)") 
} 

}

関連する問題