flutter background service for iOS

+1 vote

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, 2023 in Flutter by Tanishqa
• 1,170 points
4,402 views

2 answers 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, 2023 by Ashwini
• 5,430 points
In my app i increase the counter every 5 minutes. For both android and IOS but so the logic is written in onStart function of Flutter_background_service's class (initService). But the same thing i want to do in IOS but i don't know how can I do this. So please help me in this.
Please help me to I am not getting the how to run it in background which method doing what and which is necessary!
When i am trying to run app on ios 11 pro.

No such module 'flutter_background_service'
0 votes
If you're problem is solved then please please help me!
answered Aug 18, 2023 by Sneh
• 150 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, 2023 in Flutter by Raji
2,132 views
0 votes
1 answer

Flutter plugin fails to build on iOS

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

answered Mar 31, 2023 in Flutter by chandru
1,137 views
0 votes
1 answer
0 votes
1 answer

How to scan for bluetooth printers and print pdf or image via bluetooth in flutter?

The user wants to scan for Bluetooth ...READ MORE

answered Apr 4, 2023 in Flutter by seena
1,294 views
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, 2023 in Flutter by Anitha
3,951 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, 2023 in Flutter by vishalini
4,312 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,230 points
867 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
1,036 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,489 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