feat: Background download on iOS (#1442)

This commit is contained in:
Afterain authored and GitHub committed 2026-08-18 18:08:53 +08:00
1 parent 9d3221ebbb
commit 99924296ef
14 files changed
+383 -26

No files matched your search

+28
View File
@@ -32,6 +32,34 @@ import Libgopeed
}
})
let locationChannel = FlutterMethodChannel(
name: "gopeed/location_keep_alive",
binaryMessenger: controller.binaryMessenger
)
locationChannel.setMethodCallHandler {
(call: FlutterMethodCall, result: @escaping FlutterResult) in
switch call.method {
case "requestPermission":
LocationKeepAliveManager.shared.requestPermission { authorized in
result(authorized)
}
case "start":
LocationKeepAliveManager.shared.start()
result(nil)
case "stop":
LocationKeepAliveManager.shared.stop()
result(nil)
default:
result(FlutterMethodNotImplemented)
}
}
GeneratedPluginRegistrant.register(with: self)
SwiftFlutterForegroundTaskPlugin.setPluginRegistrantCallback(registerPlugins)
+10
View File
@@ -106,5 +106,15 @@
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
<key>UIBackgroundModes</key>
<array>
<string>location</string>
</array>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Please go to Settings and change the location permission to "Always" so that the app can continue running in the background.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Please go to Settings and change the location permission to "Always" so that the app can continue running in the background.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Please go to Settings and change the location permission to "Always" so that the app can continue running in the background.</string>
</dict>
</plist>
@@ -0,0 +1,126 @@
import Foundation
import CoreLocation
final class LocationKeepAliveManager: NSObject, CLLocationManagerDelegate {
static let shared = LocationKeepAliveManager()
private let locationManager = CLLocationManager()
private(set) var isRunning = false
private var permissionCompletion: ((Bool) -> Void)?
override init() {
super.init()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.pausesLocationUpdatesAutomatically = false
if #available(iOS 9.0, *) {
locationManager.allowsBackgroundLocationUpdates = true
}
if #available(iOS 14.0, *) {
locationManager.showsBackgroundLocationIndicator = true
}
}
func requestPermission(completion: @escaping (Bool) -> Void) {
let status = CLLocationManager.authorizationStatus()
switch status {
case .authorizedAlways:
completion(true)
case .authorizedWhenInUse:
completion(false)
case .denied, .restricted:
completion(false)
case .notDetermined:
permissionCompletion = completion
locationManager.requestAlwaysAuthorization()
@unknown default:
completion(false)
}
}
func start() {
guard !isRunning else {
return
}
let status = CLLocationManager.authorizationStatus()
guard status == .authorizedAlways else {
return
}
if #available(iOS 9.0, *) {
locationManager.allowsBackgroundLocationUpdates = true
}
locationManager.startUpdatingLocation()
isRunning = true
}
func stop() {
locationManager.stopUpdatingLocation()
isRunning = false
}
private func handleAuthorizationStatus(
_ status: CLAuthorizationStatus
) {
switch status {
case .authorizedAlways:
permissionCompletion?(true)
permissionCompletion = nil
case .authorizedWhenInUse,
.denied,
.restricted:
permissionCompletion?(false)
permissionCompletion = nil
case .notDetermined:
break
@unknown default:
permissionCompletion?(false)
permissionCompletion = nil
}
}
@available(iOS 14.0, *)
func locationManagerDidChangeAuthorization(
_ manager: CLLocationManager
) {
handleAuthorizationStatus(manager.authorizationStatus)
}
func locationManager(
_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus
) {
handleAuthorizationStatus(status)
}
func locationManager(
_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]
) {
}
func locationManager(
_ manager: CLLocationManager,
didFailWithError error: Error
) {
}
}