WatchKit WatchOS - 定位服务提示从未显示在 Apple Watch 上 [英] WatchKit WatchOS - Location Services Prompt never displayed on the Apple Watch

查看:60
本文介绍了WatchKit WatchOS - 定位服务提示从未显示在 Apple Watch 上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从 Watch 应用中调用 requestWhenInUseAuthorization() 时,位置服务提示永远不会显示给用户:无论是在 Watch 应用中,还是在 iPhone 应用中.

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.

  • 未选中Support Running without iOS App Installation(如果我选中它,提示会正确显示,但我不希望我的应用在没有 iOS 配套应用的情况下运行).
  • 我在 iPhone 应用和 Watch 扩展的 info.plist 中添加了 NSLocationWhenInUseUsageDescription 键.
  • 我还尝试在后台模式下添加定位服务(适用于 iPhone 和 Watch Extension).
  • 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).

我们如何在手表上显示提示?这甚至可能吗?我不一定要让我的用户打开他们的 iPhone 应用来管理应用中的请求位置.

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)
    }
}

推荐答案

iPhone 和 Watch 共享隐私>位置服务设置.如果应用在 iPhone 上已经有授权状态,它就不会在 Watch 上再次询问.

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.

然后打开 Watch 应用.

Then open the Watch app.

请求的步骤

新项目>WatchOS>带有WatchApp的iOS应用

New Project>WatchOS>iOS App with WatchApp

填写产品名称,无其他变化

Filled in product name, no other changes

保存在 Mac 文件夹中

Saved in a Mac folder

替换 struct ContentView &struct ContentView_Previews 与您的代码.

Replaced struct ContentView & struct ContentView_Previews with your code.

ContentView.swift 文件位于 AppName 文件夹下.

The ContentView.swift file was under the AppName folder.

添加到上述文件的Target Membership AppName WatchKit Extension.我在 File Inspector 中勾选了这个框.

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

转到 AppName WatchKit Extension 文件夹下的 ContentView.swift 文件并删除(取消选中)该文件的 Target MembershipAppName WatchKit 扩展.(如果你不这样做,你将有一个重复)

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)

AppName WatchKit Extension文件夹下的Info.plist中添加了NSLocationWhenInUseUsageDescription.

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

确保选择了 AppName WatchKit App 方案.

按下播放.等到加载完成...

Pressed Play. Waited until done loading…

按下请求位置"按钮

这篇关于WatchKit WatchOS - 定位服务提示从未显示在 Apple Watch 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆