仅在 Apple Watch 上请求位置,配对的没有代码 phone
Request location on the Apple Watch only, without code on the paired phone
我到处都看过,包括 Apple's sample app。
但是我找不到任何地方可以请求位置而不需要 phone 上的任何代码 运行,下面的代码在 'interface controller' returns a [=21 中使用时=]状态。我确实检查了我的 info.plist 是否设置了隐私密钥值。
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>
用户只能在 iPhone 上授予对其位置的访问权限。它不能在 Apple Watch 上完成。如果手表连接的iPhone解锁,phone上会提示请求位置使用授权;在 iOS.
上,您不需要 运行 任何代码
来自App Programming Guide for watchOS: Leveraging iOS Technologies
Be aware that permission for some technologies must be accepted on the
user’s iPhone. The user must grant permission to use specific system
technologies, such as Core Location. Using one of these technologies
in your WatchKit extension triggers the appropriate prompt on the
user’s iPhone. Apple Watch also displays a prompt of its own, asking
the user to view the permission request on the iPhone. For information
about the technologies that require user permission, see “Supporting
User Privacy” in App Programming Guide for iOS.
我让它工作(使用 Xcode 10.2.1、iOS 12.4、WatchOS 5.3),但只有在添加必要的键(NSLocationAlwaysAndWhenInUseUsageDescription
和 NSLocationWhenInUseUsageDescription
) 到 Info.plist
用于 iPhone 应用程序。最初我只在 Watch App Extension 中定义了它们,在那种情况下,位置授权请求在 phone 上什么也没有显示。将它们添加到 iPhone Info.plist 后,当我从 Watch 应用程序请求授权时,我在 iPhone 上获得了预期的授权对话框,而没有向 iPhone 添加任何其他代码应用程序。
可能是之前版本的bug,也可能是使用了deprecated versions of the key names?
从 watchOS 6.0/Xcode11 开始,现在可以做到这一点。要启用它,手表扩展目标必须选中 Supports running without iOS App installation
框
检查此设置并重建后,我的 requestWhenInUseAuthorization
调用按预期在表盘上显示了提示。
我到处都看过,包括 Apple's sample app。 但是我找不到任何地方可以请求位置而不需要 phone 上的任何代码 运行,下面的代码在 'interface controller' returns a [=21 中使用时=]状态。我确实检查了我的 info.plist 是否设置了隐私密钥值。
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>
用户只能在 iPhone 上授予对其位置的访问权限。它不能在 Apple Watch 上完成。如果手表连接的iPhone解锁,phone上会提示请求位置使用授权;在 iOS.
上,您不需要 运行 任何代码来自App Programming Guide for watchOS: Leveraging iOS Technologies
Be aware that permission for some technologies must be accepted on the user’s iPhone. The user must grant permission to use specific system technologies, such as Core Location. Using one of these technologies in your WatchKit extension triggers the appropriate prompt on the user’s iPhone. Apple Watch also displays a prompt of its own, asking the user to view the permission request on the iPhone. For information about the technologies that require user permission, see “Supporting User Privacy” in App Programming Guide for iOS.
我让它工作(使用 Xcode 10.2.1、iOS 12.4、WatchOS 5.3),但只有在添加必要的键(NSLocationAlwaysAndWhenInUseUsageDescription
和 NSLocationWhenInUseUsageDescription
) 到 Info.plist
用于 iPhone 应用程序。最初我只在 Watch App Extension 中定义了它们,在那种情况下,位置授权请求在 phone 上什么也没有显示。将它们添加到 iPhone Info.plist 后,当我从 Watch 应用程序请求授权时,我在 iPhone 上获得了预期的授权对话框,而没有向 iPhone 添加任何其他代码应用程序。
可能是之前版本的bug,也可能是使用了deprecated versions of the key names?
从 watchOS 6.0/Xcode11 开始,现在可以做到这一点。要启用它,手表扩展目标必须选中 Supports running without iOS App installation
检查此设置并重建后,我的 requestWhenInUseAuthorization
调用按预期在表盘上显示了提示。