前提・実現したいこと
Swift5.1でマップ上に現在の位置情報を取得するテストアプリを作成中です。
なぜかinfo.plistでも適切に設定を行っているにも関わらず位置情報の権限ダイアログが表示させることができません。
発生している問題・エラーメッセージ
021-01-29 00:45:46.412856+0900 testmap[3523:1049265] Metal API Validation Enabled 2021-01-29 00:45:46.476389+0900 testmap[3523:1049265] This app has attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an “NSLocationWhenInUseUsageDescription” key with a string value explaining to the user how the app uses this data 2021-01-29 00:45:46.476790+0900 testmap[3523:1049265] This app has attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain both “NSLocationAlwaysAndWhenInUseUsageDescription” and “NSLocationWhenInUseUsageDescription” keys with string values explaining to the user how the app uses this data not determined 2021-01-29 00:45:48.603680+0900 testmap[3523:1049442] [MKCoreLocationProvider] CLLocationManager(<CLLocationManager: 0x2831b6ab0>) for <MKCoreLocationProvider: 0x2801b1440> did fail with error: Error Domain=kCLErrorDomain Code=1 "(null)"
該当のソースコード
swift
1// 2// ViewController.swift 3// testmap 4// 5// Created by BWP179 on 2021/01/28. 6// 7 8import UIKit 9import CoreLocation 10import MapKit 11 12class ViewController: UIViewController { 13 14 @IBOutlet weak var mapView: MKMapView! 15 16 var locationManager: CLLocationManager? 17 18 19 override func viewDidLoad() { 20 super.viewDidLoad() 21 // Do any additional setup after loading the view. 22 locationManager = CLLocationManager() 23 locationManager?.delegate = self 24 locationManager?.requestWhenInUseAuthorization() 25 locationManager?.requestAlwaysAuthorization() 26 } 27// func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { 28// print("起動に失敗しました") 29// } 30// 31// func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 32// if locations.first != nil { 33// print(locations) 34// } 35// } 36} 37 38extension ViewController: CLLocationManagerDelegate{ 39 func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 40 switch status { 41 case .denied: 42 print("Dined") 43 case .notDetermined: 44 print("not determined") 45 case .authorizedWhenInUse: 46 print("when in use") 47 locationManager?.requestLocation() 48 49 case .authorizedAlways: 50 print("always") 51 case .restricted: 52 print("restricted") 53 default : 54 print("didchange") 55 56 } 57 } 58 59 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 60 print("LocationManager didUpdateLocations: numberOfLocation: (locations.count)") 61 let dateFormatter = DateFormatter() 62 dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" 63 64 locations.forEach { (location) in 65 print("LocationManager didUpdateLocations: (dateFormatter.string(from: location.timestamp)); (location.coordinate.latitude), (location.coordinate.longitude)") 66 print("LocationManager altitude: (location.altitude)") 67 print("LocationManager floor?.level: (location.floor?.level)") 68 print("LocationManager horizontalAccuracy: (location.horizontalAccuracy)") 69 print("LocationManager verticalAccuracy: (location.verticalAccuracy)") 70 print("LocationManager speedAccuracy: (location.speedAccuracy)") 71 print("LocationManager speed: (location.speed)") 72 print("LocationManager timestamp: (location.timestamp)") 73 print("LocationManager courseAccuracy: (location.courseAccuracy)") // 13.4 74 print("LocationManager course: (location.course)") 75 } 76 } 77 78 func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { 79 print("LocationManager didFailWithError (error.localizedDescription)") 80 if let error = error as? CLError, error.code == .denied { 81 // Location updates are not authorized. 82 // To prevent forever looping of `didFailWithError` callback 83 locationManager?.stopMonitoringSignificantLocationChanges() 84 return 85 } 86 } 87} 88
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>NSLocationAlwaysAndWhenInUseUsageDescription</key> <string>test</string> <key>NSLocationWhenInUseUsageDescription</key> <string>test</string> <key>NSLocationTemporaryUsageDescriptionDictionary</key> <dict/> <key>CFBundleDevelopmentRegion</key> <string>$(DEVELOPMENT_LANGUAGE)</string> <key>CFBundleExecutable</key> <string>$(EXECUTABLE_NAME)</string> <key>CFBundleIdentifier</key> <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundleName</key> <string>$(PRODUCT_NAME)</string> <key>CFBundlePackageType</key> <string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string> <key>CFBundleShortVersionString</key> <string>1.0</string> <key>CFBundleVersion</key> <string>1</string> </dict> </plist>
Info.plist に、保護されたリソースへのアクセスが必要な理由を、以下のキー
NSLocationWhenInUseUsageDescription
NSLocationAlwaysAndWhenInUseUsageDescription
に対して記述する必要があります。
https://developer.apple.com/jp/documentation/uikit/protecting_the_user_s_privacy/requesting_access_to_protected_resources/
回答ありがとうございます。
一応 下記のようにinfo.plistの編集は行っております。
testではビルドが通らないという話でしょうか?
実装上、文言を審査されるという話は知らないのですが、
```
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>test</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>test</string>
```
NSLocationAlwaysAndWhenInUseUsageDescription
NSLocationWhenInUseUsageDescription
この2つが追加されていれば、文言に関係なく動くと思います(動きました)。
Info.plist を直接いじらず、Xcode 上で編集しています。
気になったのは、
<key>NSLocationTemporaryUsageDescriptionDictionary</key>
<dict/>
と、変なところで<dict>が閉じているのですが、これは単なるコピペミスでしょうか(ちなみに、上記のplistのままだと動かないことも確認しています)。
すみませんこちら解決いたしました。
<dict/>を削除したところ、問題なく動作いたしました。ありがとうございます。
回答1件
あなたの回答
tips
プレビュー