gpt4 book ai didi

ios - 仅在 Apple Watch 上请求位置,无需配对手机上的代码

转载 作者:IT王子 更新时间:2023-10-29 05:42:25 25 4
gpt4 key购买 nike

我到处都看过,包括Apple's sample app .但是我找不到任何地方可以请求位置而不需要在手机上运行任何代码,下面的代码在“接口(interface) Controller ”中使用时返回“未确定”状态。我确实检查了我的 info.plist 是否设置了隐私 key 值。

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

}
}
}

主应用信息.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>

观看扩展信息.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 上完成。如果Watch连接的iPhone处于解锁状态,手机会提示请求位置使用授权;您无需在 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.

关于ios - 仅在 Apple Watch 上请求位置,无需配对手机上的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47543655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com