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

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

问题描述

当我从Watch App调用 requestWhenInUseAuthorization()时,Location Services提示从不会显示给用户:在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.

  • 未选中无需安装iOS应用即可运行的支持(如果我选中它,则提示会正确显示,但我不希望我的应用在没有iOS配套应用的情况下可以正常工作).
  • 我在iPhone应用程序和Watch Extension的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和手表共享隐私">位置服务设置.如果该应用程序已经在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.

设置设置">隐私">"LocationServices">"YourApp">允许位置访问">下一次询问".

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

然后打开监视"应用.

请求的步骤

New Project> WatchOS>带有WatchApp的iOS应用

New Project>WatchOS>iOS App with WatchApp

填写产品名称,没有其他更改

Filled in product name, no other changes

保存在Mac文件夹中

替换了 struct ContentView &使用您的代码构造ContentView_Previews .

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

添加到上述文件的 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成员身份 AppName 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天全站免登陆