首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >locationManager.requestWhenInUseAuthorization()没有位置权限弹出

locationManager.requestWhenInUseAuthorization()没有位置权限弹出
EN

Stack Overflow用户
提问于 2022-01-27 03:04:37
回答 1查看 195关注 0票数 -2

我遵循一个在线教程来制作一个简单的项目,在地图上显示用户的位置。但是,当我运行它时,不会显示用户位置,也不会弹出请求位置权限。我不知道我怎么能解决这个问题。我已经把密码附在下面。

代码语言:javascript
复制
import MapKit
import SwiftUI

struct ContentView: View {
    @StateObject private var viewModel=ContentViewModel()
    
    var body: some View {
        Map(coordinateRegion: $viewModel.region)
            .ignoresSafeArea()
            .onAppear{viewModel.checkIfLocationServicesIsEnabled()}
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

final class ContentViewModel: NSObject, ObservableObject, CLLocationManagerDelegate{
    @Published var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude:37.3331516, longitude: -121.891054), span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1));
    var locationManager: CLLocationManager?
    func checkIfLocationServicesIsEnabled(){
        if CLLocationManager.locationServicesEnabled(){
            locationManager=CLLocationManager()
            locationManager!.delegate=self
        }
        else{
            print("Turn location on")
        }
    }
    private func checkLocationAuthorization(){
        guard let locationManager = locationManager else {
            return
        }
        switch locationManager.authorizationStatus{
        case.notDetermined:
            locationManager.requestWhenInUseAuthorization()
        case .restricted:
            print("Your location is restricted likely due to parental controls.")
        case .denied:
            print("You have denied this app location permission. Go into settings to change it.")
        case .authorizedAlways:
            region=MKCoordinateRegion(center: locationManager.location!.coordinate, span:MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
        case .authorizedWhenInUse:
            region=MKCoordinateRegion(center: locationManager.location!.coordinate, span:MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
        @unknown default:
            break
        }
        func locationManagerDidChangeAuthorization(_manager: CLLocationManager) {
            checkLocationAuthorization()
            
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2022-01-27 09:41:33

您的代码有两个问题

您不是要求permissions.

  • You实现委托错误的
  1. .

这应该是可行的:

代码语言:javascript
复制
final class ContentViewModel: NSObject, ObservableObject, CLLocationManagerDelegate{
    @Published var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude:37.3331516, longitude: -121.891054), span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1));
    var locationManager: CLLocationManager?
    
   
    func checkIfLocationServicesIsEnabled(){
        if CLLocationManager.locationServicesEnabled(){
            locationManager=CLLocationManager()
            locationManager?.delegate=self
            //ask for permissions here
            //depending on your usage you could also ask for 
            //locationManager?.requestWhenInUseAuthorization()
            locationManager?.requestAlwaysAuthorization()
        }
        else{
            print("Turn location on")
        }
    }
    private func checkLocationAuthorization(){
        guard let locationManager = locationManager else {
            return
        }
        switch locationManager.authorizationStatus{
        case.notDetermined:
            //the function containing this will only be called if the permission get requested so remove it
            //locationManager.requestWhenInUseAuthorization()
        case .restricted:
            print("Your location is restricted likely due to parental controls.")
        case .denied:
            print("You have denied this app location permission. Go into settings to change it.")
        case .authorizedAlways:
            region=MKCoordinateRegion(center: locationManager.location!.coordinate, span:MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
        case .authorizedWhenInUse:
            region=MKCoordinateRegion(center: locationManager.location!.coordinate, span:MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
        @unknown default:
            break
        }
    }
    // this function was wrapped inside your checkLocationAuthorization() function
    //move it to outer scope. It gets called every time you request permissions
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        checkLocationAuthorization()
        
    }
}

请注意,您将不得不添加多个隐私密钥,以使这一工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70872834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档