【问题标题】:Request location on the Apple Watch only, without code on the paired phone仅在 Apple Watch 上请求位置,在配对手机上没有代码
【发布时间】:2018-05-12 14:57:28
【问题描述】:

我到处找,包括Apple's sample app。 但是当我不需要在手机上运行任何代码就可以请求位置时,我找不到任何地方,下面的代码在“接口控制器”中使用时返回“未确定”状态。我确实检查了我的 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

        }
    }
}

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

【问题讨论】:

  • 您找到解决方案了吗?我也有同样的情况。
  • @kolczak 我还没有,准备回来。如果你有什么发现,请告诉我。

标签: ios swift core-location apple-watch


【解决方案1】:

用户只能在 iPhone 上授予对其位置的访问权限。它不能在 Apple Watch 上完成。如果 Watch 连接的 iPhone 解锁,手机会显示请求位置使用授权的提示;您无需在 iOS 上为此运行任何代码。

来自App Programming Guide for watchOS: Leveraging iOS Technologies

请注意,某些技术的许可必须在 用户的 iPhone。用户必须授予使用特定系统的权限 技术,例如核心位置。使用其中一种技术 在您的 WatchKit 扩展中触发适当的提示 用户的 iPhone。 Apple Watch 还会显示自己的提示,询问 用户在 iPhone 上查看权限请求。有关信息 有关需要用户许可的技术,请参阅“支持 iOS 应用编程指南中的“用户隐私”。

【讨论】:

  • 我仍然认为我遗漏了一些东西..我在手表上请求whenInUseStatus,但没有提示在 iPhone 上弹出位置提示请求?你确定不需要在 iPhone 上运行代码吗?
  • 您确定您也为您的 Watch 目标设置了正确的 Info.plist 文件,而不仅仅是为您的 iPhone 目标吗?是的,甚至文档也明确指出,无需任何 iOS 代码即可在 iPhone 上显示提示。
  • 我很确定,我将 info.plist 值添加到我的问题中。
【解决方案2】:

从 watchOS 6.0/Xcode 11 开始,这已经成为可能。要启用它,监视扩展目标必须选中 Supports running without iOS App installation 复选框

检查此设置并重建后,我的requestWhenInUseAuthorization 呼叫按预期在表盘上显示了提示。

【讨论】:

  • 我也设法让它工作了。但是每次有 WatchOS 更新时,它都会停止工作。并且由于某种原因再次请求它不起作用,所以我必须删除该应用程序并重新安装它才能显示弹出窗口。
【解决方案3】:

我让这个工作(使用 Xcode 10.2.1、iOS 12.4、WatchOS 5.3),但只有在将必要的密钥(NSLocationAlwaysAndWhenInUseUsageDescriptionNSLocationWhenInUseUsageDescription)添加到 iPhone 应用程序的Info.plist 之后。最初我只在 Watch App Extension 中定义了它们,在这种情况下,位置授权请求在手机上什么也没有显示。将它们添加到 iPhone Info.plist 后,当我从 Watch 应用程序请求授权时,我在 iPhone 上得到了预期的授权对话框,而无需向 iPhone 应用程序添加任何其他代码。

这可能是以前版本中的错误,或者可能是使用了deprecated versions of the key names

【讨论】:

  • 我按照这些步骤操作,但我看不到提示(Xcode 12.3、iOS 14、WatchOS 5/6/7)...您最近有遇到什么问题吗?在这里查看我的完整问题:stackoverflow.com/q/65850886/2681506
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-14
  • 2016-01-30
  • 2016-05-02
  • 2020-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多