Files
shaguabijia-app-ios/App/Common/LocationManager.swift
T
marco 98b7600797 初始化傻瓜比价正式版 iOS 客户端
对标安卓正式版、对接 app-server(8770)的 SwiftUI 客户端。iOS 无障碍受限、无比价核心,做自成一体:美团 CPS 导购 + 福利(签到/任务/金币)+ 查看(比价记录/省钱战绩)。
功能完整 M0-M5:脚手架、短信登录(JWT+Keychain+401 自动刷新)、首页券 feed 导购、比价记录+省钱战绩、福利、我的(资料/反馈/设置/注销)。
单 target、0 三方依赖、XcodeGen 生成工程;Debug/Release 双 plist 分流 ATS。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-05 00:54:11 +08:00

74 lines
2.8 KiB
Swift

import CoreLocation
/// CoreLocation async :"使"
/// // nil,()
@MainActor
final class LocationManager: NSObject, CLLocationManagerDelegate {
private let manager = CLLocationManager()
private var authContinuation: CheckedContinuation<Bool, Never>?
private var locContinuation: CheckedContinuation<CLLocationCoordinate2D?, Never>?
override init() {
super.init()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyHundredMeters
}
/// notDetermined ,;/ nil
func requestOnce() async -> CLLocationCoordinate2D? {
// :Tab HomeView .task , continuation
guard authContinuation == nil, locContinuation == nil else { return nil }
switch manager.authorizationStatus {
case .denied, .restricted:
return nil
case .notDetermined:
guard await requestAuthorization() else { return nil }
default:
break
}
return await fetchLocation()
}
private func requestAuthorization() async -> Bool {
await withCheckedContinuation { cont in
authContinuation = cont
manager.requestWhenInUseAuthorization()
}
}
private func fetchLocation() async -> CLLocationCoordinate2D? {
await withCheckedContinuation { cont in
locContinuation = cont
manager.requestLocation()
}
}
// MARK: - CLLocationManagerDelegate(线, MainActor;continuation nil resume)
nonisolated func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
let status = manager.authorizationStatus
Task { @MainActor in
guard let cont = authContinuation else { return } // init :cont ,
authContinuation = nil
cont.resume(returning: status == .authorizedWhenInUse || status == .authorizedAlways)
}
}
nonisolated func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let coord = locations.first?.coordinate
Task { @MainActor in
guard let cont = locContinuation else { return }
locContinuation = nil
cont.resume(returning: coord)
}
}
nonisolated func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
Task { @MainActor in
guard let cont = locContinuation else { return }
locContinuation = nil
cont.resume(returning: nil)
}
}
}