简体   繁体   English

CLLocationManager requestLocation 按钮在 iOS 15 Xcode 13 Beta 2 上抛出错误

[英]CLLocationManager requestLocation button is throwing errors on iOS 15 Xcode 13 Beta 2

I am trying to request one time access to the location.我正在尝试请求一次访问该位置。 I have made the necessary additions to the info.plist file as shown below:我已经对 info.plist 文件进行了必要的添加,如下所示:

在此处输入图片说明

Now, when I use the following code to call updateLocation I get the following error:现在,当我使用以下代码调用 updateLocation 时,出现以下错误:

[MKCoreLocationProvider] CLLocationManager(<CLLocationManager: 0x600001cf4470>) for <MKCoreLocationProvider: 0x600002ce03f0> did fail with error: Error Domain=kCLErrorDomain Code=1 "(null)"
The operation couldn’t be completed. (kCLErrorDomain error 1.)

I also never got the prompt for allowing the app to use my location.我也从未收到允许应用程序使用我的位置的提示。

Here is the complete implementation of the LocationManager class.这是 LocationManager 类的完整实现。

class LocationManager: NSObject, ObservableObject {
    
    private let locationManager = CLLocationManager()
    @Published var region = MKCoordinateRegion.defaultRegion
    
    static let defaultDistance: CLLocationDistance = 1000000
    
    override init() {
        super.init()
        
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = kCLDistanceFilterNone
        locationManager.delegate = self
    }
    
    func updateLocation() {
        locationManager.requestLocation()
    }
}

extension LocationManager: CLLocationManagerDelegate {
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        
        guard let location = locations.last else { return }
        
        DispatchQueue.main.async {
            
            self.region = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: Self.defaultDistance, longitudinalMeters: Self.defaultDistance)
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error.localizedDescription)
    }
}

I am using Xcode 13 Beta 2 and running on a simulator.我正在使用 Xcode 13 Beta 2 并在模拟器上运行。

Do add the request method:添加请求方法:

locationManager.requestWhenInUseAuthorization()

And to observe authorization status add this delegate method too:并观察授权状态也添加此委托方法:

extension ViewController: CLLocationManagerDelegate {
  func locationManager(_
    manager: CLLocationManager,
    didChangeAuthorization status: CLAuthorizationStatus) {
    switch status {
      case .authorizedWhenInUse:
        print("Authorized When in Use")
      case .authorizedAlways:
        print("Authorized Always")
      case .denied:
        print("Denied")
      case .notDetermined:
        print("Not determined")
      case .restricted:
        print("Restricted")
      @unknown default:
        print("Unknown status")
    }
  }
}

If you are getting this error in simulator make sure simulating location is activated如果您在模拟器中遇到此错误,请确保模拟位置已激活

Target>Edit Scheme>Run>Options>Allow Location Simulation

And

Simulator>Features>Location>City Run

In my case I added the CLLocationButton to my child view controller (which also conformed to CLLocationManagerDelegate).就我而言,我将 CLLocationButton 添加到我的子视图控制器(也符合 CLLocationManagerDelegate)。 In this setup the popup never showed up.在此设置中,弹出窗口从未出现。

Adding the button directly to parent view controller solved the issue, popup shows up.将按钮直接添加到父视图控制器解决了问题,弹出窗口出现。

No clue why, perhaps CoreLocation framework is somehow searching for the top view controller, but the hierarchy gets broken.不知道为什么,也许 CoreLocation 框架正在以某种方式搜索顶视图控制器,但是层次结构被破坏了。 I failed to replicate the same behaviour in a simple example project.我未能在一个简单的示例项目中复制相同的行为。 (Xcode 13 beta 5) (Xcode 13 测试版 5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在iOS 8或更旧版本上实现CLLocationManager.requestLocation()? - How to implement CLLocationManager.requestLocation() on iOS 8 or older? iPhone XS 设备上 iOS 15 Beta 版“不受信任的开发者”错误上的 Xcode 13 - Xcode 13 on iOS 15 Beta 'Untrusted Developer' error on iPhone XS device Xcode 11 beta 5 中没有 iOS 13 模拟器 - No iOS 13 simulator in Xcode 11 beta 5 "Xcode 13 - 缺少 IOS 15 模拟器?" - Xcode 13 - Missing IOS 15 simulator? EmptyView 未在 iOS 15、Xcode 13 中显示 - EmptyView is not showing in iOS 15, Xcode 13 我可以为 Xcode 11 beta 5 下载 iOS 13 beta 6 模拟器吗? - Can I download iOS 13 beta 6 simulator for Xcode 11 beta 5? 应用卡在带有Xcode 11 beta的iOS 13 beta模拟器上,但在带有Xcode 10的iOS 13 beta模拟器上运行良好 - App stuck on iOS 13 beta simulator with Xcode 11 beta but working fine on iOS 13 beta simulator with Xcode 10 iOS 15 - Xcode 13-RC 警告:-[NSKeyedUnarchiver validateAllowedClass:forKey:] - iOS 15 - Xcode 13-RC warning: -[NSKeyedUnarchiver validateAllowedClass:forKey:] 使用 Xcode 13 选择退出 iOS 15 的黑暗模式 - Opting out of Dark Mode with iOS 15 using Xcode 13 Xcode 13 iOS 15 编程 Obj-C 约束 translatesAutoresizingMaskIntoConstraints - Xcode 13 iOS 15 Programmatic Obj-C Constraints translatesAutoresizingMaskIntoConstraints
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM