flutter background service for iOS

0 votes

I am doing a background fetch every 15 mins and I also understand that this only keeps the app alive for about 15-30 seconds. In that 15-30 seconds, I am calling an API to fetch current location. I was wondering if I am using a package "flutter_background_service" to handle my background and foreground tasks, do I still need to implement and write a separate code for BGTaskScheduler in my AppDelegate.swift file? Or the package will help me with that?

Apr 4 in Flutter by Tanishqa
• 1,000 points
744 views

1 answer to this question.

0 votes

The flutter_background_service package provides a convenient way to run background tasks on both Android and iOS platforms in Flutter. However, for iOS platform, you still need to implement the BGTaskScheduler in your AppDelegate.swift file to handle background tasks.

The BGTaskScheduler is an iOS framework that allows you to schedule and handle background tasks on your app. It is responsible for scheduling your app's background fetch tasks and launching your app in the background to perform those tasks.

To use BGTaskScheduler with flutter_background_service package, you can create a custom task that will be scheduled by BGTaskScheduler and then call the start() method provided by the flutter_background_service package to start the task.

Here is an example code snippet:

import UIKit
import Flutter
import flutter_background_service

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var flutterEngine : FlutterEngine?;
    var task: UIBackgroundTaskIdentifier = .invalid
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        flutterEngine = FlutterEngine(name: "my_engine")
        flutterEngine?.run()
        
        return true
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        task = UIApplication.shared.beginBackgroundTask(withName: "myBackgroundTask") {
            self.endTask()
        }
        
        BackgroundService.initialize(isolateName: "myIsolate", callback: myTask)
        BackgroundService.registerHeadlessTask()
        BackgroundTaskScheduler.init().register(
            forTaskWithIdentifier: "myBackgroundTask",
            using: nil) { task in
            self.handleAppRefreshTask(task: task as! BGAppRefreshTask)
        }
    }
    
    func handleAppRefreshTask(task: BGAppRefreshTask) {
        task.expirationHandler = {
            self.endTask()
        }
        
        BackgroundService.performTask()
        
        task.setTaskCompleted(success: true)
        self.endTask()
    }
    
    func myTask() {
        // Perform your background task here
        // ...
        
        BackgroundService.finish(taskId: task)
    }
    
    func endTask() {
        UIApplication.shared.endBackgroundTask(task)
        task = .invalid
    }
}

In this example, the handleAppRefreshTask function is registered as the handler for the BGAppRefreshTask task. It calls the myTask function provided by flutter_background_service package to perform the actual background task.

Note that you will also need to add the BGTaskSchedulerPermittedIdentifiers key to your app's Info.plist file and include the identifier for your custom task to allow BGTaskScheduler to schedule your task.

<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
    <string>myBackgroundTask</string>
</array>

With this implementation, your background task will be automatically scheduled and launched by iOS at the specified interval, and you can use the flutter_background_service package to perform your background tasks in a separate isolate.

answered Apr 4 by Ashwini
• 5,380 points

Related Questions In Flutter

0 votes
1 answer

Run IOS Background Services for longer time

To run an iOS app's background services ...READ MORE

answered Mar 31 in Flutter by Raji
362 views
0 votes
1 answer

Flutter plugin fails to build on iOS

It looks like the error is related ...READ MORE

answered Mar 31 in Flutter by chandru
352 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to disable bouncing animation for pageview in flutter

To disable bouncing animation for PageView in ...READ MORE

answered Apr 10 in Flutter by Anitha
411 views
0 votes
1 answer

flutter app not running on ios simulator after adding firebase notification

The error message you provided seems to ...READ MORE

answered Apr 10 in Flutter by vishalini
1,726 views
0 votes
1 answer

What is the future of flutter?

Hi@MD, Flutter is a rather new cross-platform framework ...READ MORE

answered Jul 17, 2020 in Others by akhtar
• 38,240 points
676 views
0 votes
1 answer

What is Flutter?

Hi@akhtar, Flutter is an app SDK for building ...READ MORE

answered Jul 17, 2020 in Others by MD
• 95,440 points
870 views
0 votes
1 answer

How to install Flutter in Windows system?

Hi@akhtar, You can follow the below-given steps to ...READ MORE

answered Jul 17, 2020 in Others by MD
• 95,440 points
1,281 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP