Request location on the Apple Watch only, without code on the paired phone

你离开我真会死。 提交于 2021-02-07 11:54:55

问题


I've looked everywhere including Apple's sample app. But I can't find anywhere when I can request location without needing any code running on the phone, the below code when used in an 'interface controller' returns a "not determined" status. I did check that my info.plist has the privacy key values set.

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>

Watch Extension 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>

回答1:


The user can only grant access to their location on their iPhone. It cannot be done on the Apple Watch. If the iPhone to which the Watch is connected is unlocked, the prompt asking for location usage authorization will be displayed on the phone; you don't need to run any code for this on iOS.

From the 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.




回答2:


As of watchOS 6.0/ Xcode 11, this is now possible. To enable it, the watch extension target must have the box checked for Supports running without iOS App installation

After checking this setting and rebuilding, my requestWhenInUseAuthorization call showed the prompt on the watch face as expected.




回答3:


I got this to work (using Xcode 10.2.1, iOS 12.4, WatchOS 5.3), but only after adding the necessary keys (NSLocationAlwaysAndWhenInUseUsageDescription and NSLocationWhenInUseUsageDescription) to Info.plist for the iPhone app. Initially I only had them defined in the Watch App Extension, and in that case, the request for location authorization displayed nothing on the phone. Once I added them to the iPhone Info.plist, I got the expected authorization dialog on the iPhone when requesting authorization from the Watch app, without adding any additional code to the iPhone app.

Perhaps this was a bug in previous versions, or perhaps it was the use of deprecated versions of the key names?



来源:https://stackoverflow.com/questions/47543655/request-location-on-the-apple-watch-only-without-code-on-the-paired-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!