You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已实现CLLocationManager代理方法仍抛出响应要求错误致App崩溃

定位请求触发App崩溃问题

当前仅需在屏幕显示位置信息,点击定位按钮后App崩溃,控制台输出如下:

2022-11-14 16:43:07.333908-0700 Countries[7257:1687502] *** Assertion failure in -[CLLocationManager requestLocation], CLLocationManager.m:1323
2022-11-14 16:43:07.334806-0700 Countries[7257:1687502] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didUpdateLocations:'
*** First throw call stack:
(0x1c4566248 0x1bd92ba68 0x1beeea81c 0x1cfd2a654 0x1040d7308 0x1040d3ff8 0x246a377e8 0x1c69396d8 0x1c69393d8 0x1c6938d6c 0x2464f75f8 0x1c6938848 0x1c681e060 0x1c709e1d8 0x1c67e1d18 0x1c67e6674 0x1c67e5944 0x1c67e5000 0x1c682cd64 0x1c7473a30 0x1c6d3b678 0x1c737a904 0x1c7379ad0 0x1c463222c 0x1c463e614 0x1c45c251c 0x1c45d7eb8 0x1c45dd1e4 0x1fd3fd368 0x1c6a8cd88 0x1c6a8c9ec 0x1c808bce8 0x1c7fe8c24 0x1c7fd1b44 0x1040e8b20 0x1040e8bc8 0x1e2901948)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didUpdateLocations:'
terminating with uncaught exception of type NSException
(lldb) 

现有代码

LocationManager类:

import Foundation
import CoreLocation
import CoreLocationUI

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
    
    let manager = CLLocationManager()
    
    @Published var location: CLLocationCoordinate2D?
    
    override init() {
        super.init()
        manager.delegate = self
        manager.requestWhenInUseAuthorization()
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation], didFailWithError error: Error) {
        location = locations.first?.coordinate
    }
    
    func requestLocation() {
        manager.requestLocation()
    }
}

视图代码:

import SwiftUI
import _CoreLocationUI_SwiftUI

struct SettingsView: View {
    
    @StateObject var locationManager = LocationManager()
    

    var body: some View {
        NavigationView {
            List {              
                VStack {
                    if let location = locationManager.location {
                        Text("Your location: \(location.latitude), \(location.longitude)")
                    }
                    
                    LocationButton {
                        locationManager.requestLocation()
                    }
                    .frame(height: 44)
                    .padding()
                    
                }
            
            }
            .navigationTitle("Settings")
        
        }
    }
}

struct SettingsView_Previews: PreviewProvider {
    static var previews: some View {
        SettingsView()
    }
}

已尝试重启项目,物理设备和模拟器上点击按钮均触发相同崩溃。


解决方案

崩溃原因是错误地将两个独立的CLLocationManagerDelegate方法合并成了一个:locationManager(_:didUpdateLocations:)和locationManager(_:didFailWithError:)是两个分开的协议方法,必须分别实现,系统找不到正确的didUpdateLocations方法才抛出异常。

修正后的LocationManager代码:

import Foundation
import CoreLocation
import CoreLocationUI

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
    
    let manager = CLLocationManager()
    
    @Published var location: CLLocationCoordinate2D?
    
    override init() {
        super.init()
        manager.delegate = self
        manager.requestWhenInUseAuthorization()
    }
    
    // 单独实现位置更新方法
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        location = locations.first?.coordinate
    }
    
    // 单独实现错误处理方法
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        // 可在此添加错误提示逻辑,比如打印错误信息
        print("定位失败:\(error.localizedDescription)")
    }
    
    func requestLocation() {
        manager.requestLocation()
    }
}

另外需确保Info.plist中添加了定位权限描述:

  • 添加NSLocationWhenInUseUsageDescription键,值为向用户说明的定位用途(比如"需要获取您的位置信息以显示当前坐标")

内容的提问来源于stack exchange,提问作者Dr. Bright

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 14:45:57