Browse Source

修复退出导航奔溃问题

newdev_shunjiawei
fdp 1 year ago
parent
commit
01fc48eeef
  1. 49
      local_plugins/amap_service/ios/Classes/AmapViewController.swift

49
local_plugins/amap_service/ios/Classes/AmapViewController.swift

@ -56,9 +56,15 @@ class AmapViewController: UIViewController {
cleanupResources() cleanupResources()
} }
/**
* 清理资源,包括移除通知观察者
*/
private func cleanupResources() { private func cleanupResources() {
print("开始清理导航资源") print("开始清理导航资源")
// 移除通知观察者,防止内存泄漏
NotificationCenter.default.removeObserver(self, name: NSNotification.Name("ExitNavigation"), object: nil)
// 停止语音合成 // 停止语音合成
speechSynthesizer.stopSpeaking(at: .immediate) speechSynthesizer.stopSpeaking(at: .immediate)
@ -144,21 +150,36 @@ class AmapViewController: UIViewController {
print("导航------------------ startNavigation_4") print("导航------------------ startNavigation_4")
} }
/**
* 退出导航并清理资源
* 添加了nil检查以防止崩溃
*/
@objc private func exitNavigation() { @objc private func exitNavigation() {
print("导航------------------ 退出导航,清理资源") print("导航------------------ 退出导航,清理资源")
// 安全检查:确保driveManager存在再进行操作
guard let manager = driveManager else {
print("⚠️ driveManager已为nil,跳过导航清理")
return
}
// 停止导航 // 停止导航
driveManager.stopNavi() manager.stopNavi()
driveManager.removeDataRepresentative(driveView)
driveManager.delegate = nil
// 移除导航视图 // 安全检查:确保driveView存在再进行操作
driveView.delegate = nil if let view = driveView {
driveView.removeFromSuperview() manager.removeDataRepresentative(view)
driveView = nil view.delegate = nil
view.removeFromSuperview()
} else {
print("⚠️ driveView已为nil,跳过视图清理")
}
// 销毁导航管理器 // 清理管理器
manager.delegate = nil
driveManager = nil driveManager = nil
driveView = nil
// 关闭当前页面 - 修复:使用正确的退出方式 // 关闭当前页面 - 修复:使用正确的退出方式
if let navigationController = navigationController { if let navigationController = navigationController {
// 如果是在导航控制器中,使用 pop // 如果是在导航控制器中,使用 pop
@ -168,13 +189,19 @@ class AmapViewController: UIViewController {
dismiss(animated: true, completion: nil) dismiss(animated: true, completion: nil)
} }
} }
/**
* 添加通知监听器
* 使用弱引用避免循环引用和对象销毁后的调用
*/
private func addNSNotification(){ private func addNSNotification(){
NotificationCenter.default.addObserver( NotificationCenter.default.addObserver(
forName: NSNotification.Name("ExitNavigation"), forName: NSNotification.Name("ExitNavigation"),
object: nil, object: nil,
queue: .main queue: .main
) { _ in ) { [weak self] _ in
self.exitNavigation() // 使用弱引用避免在对象销毁后调用方法
self?.exitNavigation()
} }
} }
} }

Loading…
Cancel
Save