WatchKit WatchOS - Location Services Prompt never displayed on the Apple Watch

怎甘沉沦 提交于 2021-01-29 20:02:28

问题


When I call requestWhenInUseAuthorization() from an Watch App, the Location Services prompt is never displayed to the user: neither in the Watch app, nor in the iPhone app.

  • The Support Running Without iOS App Installation is not checked (if I check it, the prompt is correctly displayed, but I don't want my app to work without an iOS companion app).
  • I added the NSLocationWhenInUseUsageDescription key in the info.plist of both the iPhone app and the Watch Extension.
  • I also tried to add the Location Services in background mode (for both the iPhone and the Watch Extension).

How can we have the prompt displayed on the Watch? Is that even possible? I don't necessarily want to ask my users to open their iPhone app to manage the request location in the app.


import SwiftUI
import CoreLocation

struct ContentView: View {
    
    @StateObject var watchLocationManager = WatchLocManager()
    
    var body: some View {
        Button("Request location") { watchLocationManager.requestAuthorization() }
            .onAppear {
                watchLocationManager.setupLocationManager()
            }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
    
class WatchLocManager: NSObject, ObservableObject {

    private var locationManager = CLLocationManager()
    
    func setupLocationManager() {
        self.locationManager.delegate = self
        self.locationManager.activityType = .otherNavigation
        self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        self.locationManager.distanceFilter = 20.0
    }
    
    func requestAuthorization() {
        self.locationManager.requestWhenInUseAuthorization()
    }
}

extension WatchLocManager: CLLocationManagerDelegate {

    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        print("⌚️ locationManagerDidChangeAuthorization: \(manager.authorizationStatus.rawValue)")
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print(locations)
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error)
    }
}

回答1:


The iPhone and Watch share Privacy>Location Services Settings. If the app already has an authorization status on the iPhone it won't ask again on the Watch.

Set the Settings>Privacy>LocationServices>YourApp>Allow Location Access>Ask Next Time.

Then open the Watch app.

Requested Steps

New Project>WatchOS>iOS App with WatchApp

Filled in product name, no other changes

Saved in a Mac folder

Replaced struct ContentView & struct ContentView_Previews with your code.

The ContentView.swift file was under the AppName folder.

ADDED to the above file’s Target Membership the AppName WatchKit Extension. I did this in the File Inspector by checking the box.

Went to the ContentView.swift file that was under the AppName WatchKit Extension folder and REMOVED (un-checking) the file’s Target Membership to AppName WatchKit Extension. (You will have a duplicate if you don’t do this)

Added NSLocationWhenInUseUsageDescription to Info.plist under the AppName WatchKit Extension Folder.

Made sure AppName WatchKit App scheme was selected.

Pressed Play. Waited until done loading…

Pressed the “Request Location” Button



来源:https://stackoverflow.com/questions/65850886/watchkit-watchos-location-services-prompt-never-displayed-on-the-apple-watch

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