3.2

2017-11-04 7 views
0

ユーザーが実行されますMainViewController3.2

override func viewDidLoad() { 
     super.viewDidLoad() 
     locationManager.delegate = self 
     locationManager.requestAlwaysAuthorization() 
     locationManager.requestWhenInUseAuthorization() 
} 

viewDidLoad()上にあり、アラートがメインの質問です

enter image description here

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key> 
     <string>Accessing user location to determine the nearest pickup location</string> 
     <key>NSLocationWhenInUseUsageDescription</key> 
     <string>Accessing user location to determine the nearest pickup location</string> 

表示されます場合にはユーザーが[許可しない]をクリックすると、プログラマチックにどのようにチェックするのですか?ユーザーがロケーションサービスへのアプリのアクセスを拒否することを選択した場合、あなたはまた、.restrictedに気を付ける必要がありますけれども、たとえば

// Example code snippet, not from apple officials 
if NSlocation is nil then I want to do some alert 
+0

https://developer.apple.com/documentation/corelocation/cllocationmanagerdelegate/1423701-locationmanager – Paulw11

答えて

2

CLLocationManager.authorizationStatus()は(.deniedになります。ユーザーがかどうかを選択したときに通知を受けることを希望した場合許可の警告でボタンを許可しない上のユーザーのクリックをチェックするCLLocationManagerのデリゲートメソッド以下

class MainViewController: CLLocationManagerDelegate{ 

    let locationManager = CLLocationManager() 

    override func viewDidLoad() -> Void{ 
     super.viewDidLoad() 
     locationManager.delegate = self 
    } 

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) -> Void{ 
     switch status{ 
      case .authorizedAlways, .authorizedWhenInUse: 
       // You're allowed to use location services 
      case .denied, .restricted: 
       // You can't use location services 
      default: 
       // This case shouldn't be hit, but is necessary to prevent a compiler error 
     } 
    } 
} 
+0

ありがとう!それは動作します – sinusGob

+0

あなたが気にしないならばもう1つの質問、ユーザーが場所を切り替えることができるように、ユーザーをアプリの設定に向けるにはどうすればいいですか? – sinusGob

+0

https://stackoverflow.com/a/5655727/2895075 –

0

使用:あなたのアプリを許可または拒否していない、あなた自身にあなたの場所マネージャのdelegateを作成し、適切なメソッドを実装することができます。

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 
     switch status { 
     case .denied: 
      print("Show you own alert here") 
      break 
     } 
    } 

は、場所の設定にユーザーをリダイレクトするには、以下の従ってください:

UIApplication.sharedApplication().openURL(NSURL(string:"prefs:root=LOCATION_SERVICES")!) 

  1. は、画像の下のように enter image description here

  2. 書き込みボタンアクションでこのコードをURLタイプ "環境設定" を追加します。