私はApple's sample appなどどこでも見ました。 しかし、電話でコードを実行しなくても場所をリクエストできる場所はどこにも見つかりません。以下のコードは「インターフェイスコントローラ」で使用すると「未定」のステータスを返します。私は自分のinfo.plistにプライバシーキー値が設定されていることを確認しました。ペアリングされた電話機のコードなしで、Appleウォッチでの場所をリクエストしてください
import Foundation
import CoreLocation
class LocationManager: NSObject, CLLocationManagerDelegate {
/// Location manager to request authorization and location updates.
let manager = CLLocationManager()
/// Flag indicating whether the manager is requesting the user's location.
var isRequestingLocation = false
var workoutLocation: CLLocation?
func requestLocation() {
guard !isRequestingLocation else {
manager.stopUpdatingLocation()
isRequestingLocation = false
return
}
let authorizationStatus = CLLocationManager.authorizationStatus()
switch authorizationStatus {
case .notDetermined:
isRequestingLocation = true
manager.requestWhenInUseAuthorization()
case .authorizedWhenInUse:
isRequestingLocation = true
manager.requestLocation()
case .denied:
print("Location Authorization Denied")
default:
print("Location AUthorization Status Unknown")
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard !locations.isEmpty else { return }
DispatchQueue.main.async {
let lastLocationCoordinate = locations.last!.coordinate
print("Lat = \(lastLocationCoordinate.latitude)")
print("Long = \(lastLocationCoordinate.longitude)")
self.isRequestingLocation = false
}
}
}
メインアプリケーションのInfo.plist
<key>NSLocationAlwaysUsageDescription</key>
<string>We will read your location while performing a workout to create a workout route</string>
<key>NSLocationUsageDescription</key>
<string>We will read your location while performing a workout to create a workout route</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>We will read your location while performing a workout to create a workout route</string>
ウォッチ延長のInfo.plist
<key>NSLocationAlwaysUsageDescription</key>
<string>We will read your location while performing a workout to create a workout route</string>
<key>NSLocationUsageDescription</key>
<string>We will read your location while performing a workout to create a workout route</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>We will read your location while performing a workout to create a workout route</string>
私はまだ何かが不足していると思っています。私は 'whenInUseStatus'を時計で要求しますが、iPhone上でポップアップするプロンプトが表示されません。 iPhoneでコードを実行する必要はありませんか? – GarySabo
iPhoneのターゲットだけでなく、ウォッチターゲットにも 'Info.plist'ファイルを正しく設定していますか?はい、ドキュメンテーションでも、プロンプトがiOSコードなしでiPhoneに表示されることが明示的に記載されています。 –
私はかなり確信しています、私は私の質問に私のinfo.plist値を追加しました。 – GarySabo