diff --git a/example/ios/Runner.xcodeproj/project.pbxproj b/example/ios/Runner.xcodeproj/project.pbxproj index fb86ac2..bb8cbb5 100644 --- a/example/ios/Runner.xcodeproj/project.pbxproj +++ b/example/ios/Runner.xcodeproj/project.pbxproj @@ -361,7 +361,7 @@ DEVELOPMENT_TEAM = 2BHAXN696Y; ENABLE_BITCODE = NO; INFOPLIST_FILE = Runner/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 13.0; + IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -491,7 +491,7 @@ DEVELOPMENT_TEAM = 2BHAXN696Y; ENABLE_BITCODE = NO; INFOPLIST_FILE = Runner/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 13.0; + IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -515,7 +515,7 @@ DEVELOPMENT_TEAM = 2BHAXN696Y; ENABLE_BITCODE = NO; INFOPLIST_FILE = Runner/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 13.0; + IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", diff --git a/ios/Classes/SwiftFlutterForegroundTaskPlugin.swift b/ios/Classes/SwiftFlutterForegroundTaskPlugin.swift index a1441bb..8c163e6 100644 --- a/ios/Classes/SwiftFlutterForegroundTaskPlugin.swift +++ b/ios/Classes/SwiftFlutterForegroundTaskPlugin.swift @@ -85,12 +85,22 @@ public class SwiftFlutterForegroundTaskPlugin: NSObject, FlutterPlugin { // ================== App Lifecycle =================== public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [AnyHashable : Any] = [:]) -> Bool { - SwiftFlutterForegroundTaskPlugin.registerAppRefresh() + UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum) + if #available(iOS 13.0, *) { + SwiftFlutterForegroundTaskPlugin.registerAppRefresh() + } + return true + } + + public func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) -> Bool { + completionHandler(.newData) return true } public func applicationDidEnterBackground(_ application: UIApplication) { - SwiftFlutterForegroundTaskPlugin.scheduleAppRefresh() + if #available(iOS 13.0, *) { + SwiftFlutterForegroundTaskPlugin.scheduleAppRefresh() + } } public func applicationWillTerminate(_ application: UIApplication) { @@ -123,12 +133,14 @@ public class SwiftFlutterForegroundTaskPlugin: NSObject, FlutterPlugin { // ============== Background App Refresh ============== public static var refreshIdentifier: String = "com.pravera.flutter_foreground_task.refresh" + @available(iOS 13.0, *) private static func registerAppRefresh() { BGTaskScheduler.shared.register(forTaskWithIdentifier: refreshIdentifier, using: nil) { task in handleAppRefresh(task: task as! BGAppRefreshTask) } } + @available(iOS 13.0, *) private static func scheduleAppRefresh() { let request = BGAppRefreshTaskRequest(identifier: refreshIdentifier) request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60) @@ -140,10 +152,12 @@ public class SwiftFlutterForegroundTaskPlugin: NSObject, FlutterPlugin { } } + @available(iOS 13.0, *) private static func cancelAppRefresh() { BGTaskScheduler.shared.cancel(taskRequestWithIdentifier: refreshIdentifier) } + @available(iOS 13.0, *) private static func handleAppRefresh(task: BGAppRefreshTask) { let queue = OperationQueue() let operation = AppRefreshOperation()