locationManager.requestWhenInUseAuthorization()

在用户按下按钮但不显示任何提示时调用.

以下是我的完整代码

import SwiftUI
import CoreLocation



struct ContentView: View {
    @ObservedObject var distanceTracker = DistanceTracker();
    var body: some View {

        Button(action: distanceTracker.enableLocServices){
            Text("Enable Location")
        }
        
    }


}



struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView()

    }

}


import CoreLocation

class DistanceTracker: NSObject, ObservableObject, CLLocationManagerDelegate {
    
    let locationManager = CLLocationManager()
    var startLocation: CLLocation!
    var lastLocation: CLLocation!
    @Published var traveledDistance = 0.0
    
    override init() {
        super.init()
        locationManager.delegate = self
    }
    
    func enableLocServices(){
        locationManager.requestWhenInUseAuthorization()
        print("bruh")
    }
    
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        switch manager.authorizationStatus {
        case .notDetermined:
            print("Location permission not asked for yet")

        case .restricted:
            print("Location usage is restricted (perhaps by parental controls or some other restriction)")
        case .denied:
            print("Location permission denied by user")
        case .authorizedAlways:
            print("Location permission granted for background and foreground use")
        case .authorizedWhenInUse:
            print("Location permission granted for foreground use only")
        @unknown default:
            print("An unknown authorization status received")
        }
    }
}

按下按钮时,它会打印"breh".编辑:另外,打印的状态是"尚未请求位置权限"

NSLocationAlwaysAndWhenInUseUsageDescription and NSLocationWhenInUseUsageDescription are both added to the info section. enter image description here

手表和手机上的定位服务都已启用(这是一款仅限WatchOS的应用程序,但无论如何,我无法让它在iOS上运行),而且之前的许可并未被拒绝.我怎么才能解决这个问题呢?谢谢!

推荐答案

您使用的是这个

Privacy - Location Always and When In Use Usage Description

您还需要添加以下内容:

Privacy - Location When In Use Usage Description

当您try 此操作时,您将看到权限弹出.

Ios相关问答推荐

Swift:从字符串目录获取带有参数的本地化字符串

Firebase SDK 10+—无法安装软件包

TrueDepth摄像机像素距离不准确

如何使用超薄material ,但没有浅色/填充?

在视图上执行手势会阻止相机操作

如何在 Flutter 应用程序中测试来自 App/Play Store 的移动深层链接?

在iOS中使用Swift,即时更改应用语言后,立即改变外观会出现问题

垂直分页 UIScrollView 高度不正确

在我的 M1 上出现 Architecture arm64 问题,因此我无法运行我的旧项目

为什么@FetchRequest 在删除时不更新和重绘视图?

UIControl 子类 - 按下时显示 UIMenu

iOS按钮和navigationLink在List中相邻

iOS 是否支持使用独立 .xib 文件创建的嵌套自定义子视图?

如何在 Swift 上的 sendSynchronousRequest 中判断 Response.statusCode

SwiftUI NavigationLink 立即加载目标视图,无需单击

iOS:使用 UIView 的 'drawRect:' 与其层的委托 'drawLayer:inContext:'

什么是伞头?

将视图控制器从一个情节提要移动或复制到另一个情节提要

为什么我需要@1x、@2x 和@3x iOS 图像?

如何创建 Select 器数组