仅在Apple Watch上请求位置,而在配对的手机上没有代码

时间:2017-11-29 01:32:42

标签: ios swift core-location apple-watch

我到处寻找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

        }
    }
}

Main App 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>

观看分机信息.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>

3 个答案:

答案 0 :(得分:10)

用户只能在iPhone上授予对其位置的访问权限。它不能在Apple Watch上完成。如果已连接手表的iPhone已解锁,则手机上将显示询问位置使用授权的提示;你不需要在iOS上运行任何代码。

来自App Programming Guide for watchOS: Leveraging iOS Technologies

  

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

答案 1 :(得分:5)

从watchOS 6.0 / Xcode 11开始,这已经成为可能。要启用它,监视扩展目标必须选中int writeText(FILE *wp, char *s) { int sum = 0; while((*s++ = fputc(*s, wp))) { sum++; if(*s == '\0') break; } return sum; }

检查此设置并重建后,我的Supports running without iOS App installation呼叫按预期在表盘上显示了提示。

screenshot

答案 2 :(得分:0)

我使用了Xcode 10.2.1,iOS 12.4,WatchOS 5.3才使它起作用,但是仅在将必要的键(NSLocationAlwaysAndWhenInUseUsageDescriptionNSLocationWhenInUseUsageDescription)添加到Info.plist iPhone应用程序。最初,我只在Watch App Extension中定义了它们,在这种情况下,位置授权请求在电话上什么也没有显示。将它们添加到iPhone Info.plist后,在从Watch应用程序请求授权时,我在iPhone上看到了预期的授权对话框,而没有在iPhone应用程序中添加任何其他代码。

也许这是以前版本中的错误,或者也许是deprecated versions of the key names的使用?