engedelyek

This commit is contained in:
sagiferi
2026-10-27 21:31:27 +01:00
parent 29528f561b
commit 4a016fb1d1
7 changed files with 137 additions and 32 deletions
+81
View File
@@ -1,8 +1,11 @@
import CoreLocation
import Flutter
import UIKit
@main
@objc class AppDelegate: FlutterAppDelegate, FlutterImplicitEngineDelegate {
private let location = OneShotLocation()
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
@@ -29,6 +32,16 @@ import UIKit
}
}
}
if let registrar = engineBridge.pluginRegistry.registrar(forPlugin: "SfzenithLocation") {
let channel = FlutterMethodChannel(name: "sfzenith/location", binaryMessenger: registrar.messenger())
channel.setMethodCallHandler { [location] call, result in
guard call.method == "getLocation" else {
result(FlutterMethodNotImplemented)
return
}
location.request(result)
}
}
}
/// Saves into the app's Documents folder, which the Files app shows under
@@ -50,3 +63,71 @@ import UIKit
return "Files › SkyContour › \(target.lastPathComponent)"
}
}
/// One location fix for the web app. The WebView's navigator.geolocation shows
/// its own "localhost would like to use your location" alert on every launch
/// (WKWebView has no API to remember the answer); the app itself already holds
/// the when-in-use permission, so ask Core Location directly.
final class OneShotLocation: NSObject, CLLocationManagerDelegate {
private let manager = CLLocationManager()
private var pending: [FlutterResult] = []
private var timer: Timer?
override init() {
super.init()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
}
/// Answers {lat, lon, accuracy, alt?} or a FlutterError (denied, unavailable, timeout).
func request(_ result: @escaping FlutterResult) {
pending.append(result)
if pending.count > 1 { return } // a fix is already on its way
timer = Timer.scheduledTimer(withTimeInterval: 20, repeats: false) { [weak self] _ in
self?.finish(FlutterError(code: "timeout", message: "No location fix", details: nil))
}
switch manager.authorizationStatus {
case .notDetermined:
manager.requestWhenInUseAuthorization()
case .authorizedWhenInUse, .authorizedAlways:
manager.requestLocation()
default:
finish(FlutterError(code: "denied", message: "Location permission denied", details: nil))
}
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
guard !pending.isEmpty else { return }
switch manager.authorizationStatus {
case .notDetermined:
break
case .authorizedWhenInUse, .authorizedAlways:
manager.requestLocation()
default:
finish(FlutterError(code: "denied", message: "Location permission denied", details: nil))
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let loc = locations.last else { return }
var out: [String: Any] = [
"lat": loc.coordinate.latitude,
"lon": loc.coordinate.longitude,
"accuracy": loc.horizontalAccuracy,
]
if loc.verticalAccuracy >= 0 { out["alt"] = loc.altitude }
finish(out)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
finish(FlutterError(code: "unavailable", message: error.localizedDescription, details: nil))
}
private func finish(_ value: Any?) {
timer?.invalidate()
timer = nil
let callbacks = pending
pending = []
callbacks.forEach { $0(value) }
}
}