Background Tasks

RSS for tag

Request the system to launch your app in the background to run tasks using Background Tasks.

Posts under Background Tasks tag

134 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

Issue in accessing internet when app is launched from BGAppRefreshTask |BGProcessingTask
Use case : We want to update our app content while the app is in the background. We want to download some json data from our backend server(HTTP GET) and update the content of the application so that next time when user launches the app he see updated content. We are using apple’s BackgroundTasks framework API, specially BGAppRefreshTask and BGProcessingTask. Pretty standard Problem → HTTP request via URLSession does not work. We register and schedule BGAppRefreshTask and BGProcessingTask. Our application is launched in the background by iOS. But when we make an HTTP request to a web server, we do not get any response callback. With default configuration, after making the request the response callback or the URLSessionDataDelegate methods are not called at all. (I also tried with google url. just to be sure the problem is not only with our backend) . Every thing works fine when I check entire flow while debugging and I am connected to Xcode and trigger a background launch by using this command e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"com.background_cloud_sync_apprefresh"] I am kind of stuck with this issue and have run out of ideas to try. So I am reaching out to expert here if you have faced any such issue on iOS. I have tried URLSession with callback as well as with URLSessionDataDelegate.
1
0
250
Nov ’23
Multiple background tasks in SwiftUI
Hello fellow developers. I'm working on adding background tasks to my SwiftUI app and running into some issues. I have added the capabilities for background modes (fetch and processing) and the correct identifiers in "Permitted background task scheduler identifiers" When I test the background tasks on my device, it seems to only schedule the first task and ignoring the rest. I can run the first background task just fine with the command: e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"spotpricesRefresh"] But it only works for the first task since no other tasks seem to be scheduled. My code looks as below: .onChange(of: phase) { if (phase == .background) { scheduleSpotpricesRefresh() scheduleEmissionsRefresh() scheduleSourcesRefresh() BGTaskScheduler.shared.getPendingTaskRequests(completionHandler: { request in print("Pending task requests: \(request)") }) } } .backgroundTask(.appRefresh("spotpricesRefresh")) { await refreshSpotprices() } .backgroundTask(.appRefresh("emissionsRefresh")) { await refreshEmissions() } .backgroundTask(.appRefresh("sourcesRefresh")) { await refreshSources() } func scheduleSpotpricesRefresh() { let request = BGAppRefreshTaskRequest(identifier: "spotpricesRefresh") request.earliestBeginDate = Date(timeIntervalSinceNow: 60 * 60) do { try BGTaskScheduler.shared.submit(request) print("Scheduling spotprice refresh task") } catch { print("Could not schedule spotprices refresh: \(error)") } } func scheduleEmissionsRefresh() { let request = BGAppRefreshTaskRequest(identifier: "emissionsRefresh") request.earliestBeginDate = Date(timeIntervalSinceNow: 60 * 62) do { try BGTaskScheduler.shared.submit(request) print("Scheduling emissions refresh task") } catch { print("Could not schedule emissions refresh: \(error)") } } func scheduleSourcesRefresh() { let request = BGAppRefreshTaskRequest(identifier: "sourcesRefresh") request.earliestBeginDate = Date(timeIntervalSinceNow: 60 * 61) do { try BGTaskScheduler.shared.submit(request) print("Scheduling sources refresh task") } catch { print("Could not schedule sources refresh: \(error)") } } func refreshSpotprices() async { do { scheduleSpotpricesRefresh() ... } catch { print("Error occurred refreshing spotprices with error: \(error)") } } func refreshEmissions() async { do { scheduleEmissionsRefresh() ... } catch { print("Error occurred refreshing emissions with error: \(error)") } } func refreshSources() async { do { scheduleSourcesRefresh() ... } catch { print("Error occurred refreshing sources with error: \(error)") } } Here is the output when debugging the app: Scheduling spotprice refresh task Scheduling emissions refresh task Scheduling sources refresh task Pending task requests: [<BGAppRefreshTaskRequest: spotpricesRefresh, earliestBeginDate: 2023-11-14 10:24:51 +0000>] I hope someone can point me in the right direction. Thank you in advance. Best regards, Casper
1
0
442
Nov ’23
What factors influence the runtime of UIApplication.shared.beginBackgroundTask()?
In the WWDC 2020 session "Background execution demystified", it was mentioned that the runtime of non-discretionary Background URL Sessions (using URLSession background transfer) is influenced mainly by factors such as the App Switcher and rate limits. Based on this information, I'm curious to know if there are specific factors that similarly affect the runtime of UIApplication.shared.beginBackgroundTask(). Are there any documented constraints or system behaviors that impact how long a background task initiated by this method can run? I would appreciate any documentation or insights that could help clarify this aspect of background task management in iOS.
2
0
316
Nov ’23
BGAppRefreshTask behavior when user disables Background App Refresh
Hello community, I am looking for information about how BGAppRefreshTasks behave in case a user has disabled the Background App Refresh for my app in the Settings app. Does disabling Background App Refresh for my app means, that my registered and schedules tasks will never run or will the system execute scheduled tasks when the app is in the foreground and has priority on system resources? My app is dependent on the data downloaded with an BGAppRefreshTask and I want to know if I have to implement alternative ways to download data, for example when the scene phase changes, if users restrict my app. Thanks for any information about this topic in advance!
1
0
544
Nov ’23
Background Tasks
Hello All, I have been having issues scheduling background tasks in iOS and was wondering if there is a repository of information for that type of task. Essentially, I just need to run a function (lets call it customFunction(), and it currently is a blank function that doesn't do anything, for testing purposes) once in a while (e.g., every 5min or so... it doesn't have to be precisely at 5min. The system can decide when to run it.) I am using the BackgroundTasks framework. The background task is being scheduled without a problem, but it's being denied execution by the system. bgRefresh-com.testing.scheduler:EECE08:[ {name: ApplicationPolicy, policyWeight: 50.000, response: {Decision: Can Proceed, Score: 0.35}} {name: DeviceActivityPolicy, policyWeight: 5.000, response: {Decision: Can Proceed, Score: 0.33}} ] sumScores:61.706667, denominator:97.540000, FinalDecision: Can Proceed FinalScore: 0.632629} 'bgRefresh-com.testing.scheduler:EECE08' CurrentScore: 0.632629, ThresholdScore: 0.805854 DecisionToRun:0 Does anyone have any feedback on the most important items to check when scheduling a background task? I can share code, if necessary.
1
0
318
Nov ’23
URLSession background does not always work correctly and when opening the application again a white screen appears
I have a project that must upload data in background and keep data going even when the app is closed/terminated. So i have a background upload of data that should start/continue when the app is unloaded from memory (user force exit). In most of the cases after killing the app background upload resumes after some time (from few seconds to 10-20 mins) and data is successfully sent. However this does not always work even as expected. When the user kills the application, background uploading may terminate too with an error «Error Domain=NSURLErrorDomain Code=-999 "(null)" UserInfo={NSURLErrorBackgroundTaskCancelledReasonKey=0» After killing the app background upload may stop without finishing/throwing error and never produce any event, and when I open the application background upload does not resume. There is no error events and there is no pending/active background tasks either on my URLSession. Can they really vanish this way? In very rare cases I have encountered the problem that when background uploading in proccess (app is fully closed and when I launch the application a white screen appears (i see no splash screen, no main view), but I still can see logs that the background tasks is working actively. After the completion of data uploading the white screen does not go away, it is necessary to re-launcth the application to solve this problem. I note that these cases are not always encountered and most of the time background upload works correctly. But I'd would like to understand how to handle these problems and what can cause this weirdness. Do I get that right, so that after the user has force exited the application and it is unloaded from memory, even if a background upload task is launhed - we have no guarantee that the task will complete and the system won't kill the background task? I guess i have to act on my own, keep an eye on my tasks and restart them if they're got vanished/terminated? Is there any advices or good practices how to handle this best? And are there any strict limits on the amount of data that can be sent in background per task? P.S I do have these capabilities enabled: Background fetch &amp; Background processing.
1
0
606
Oct ’23
BGProcessingTask expirationHandler - Do i MUST finish background work? or will it can be resumed later?
I'm building a BGProcessingTask that may take more then 5 minutes to complete. The processing task runs on a Singleton object that can be accessed (or even started) when the app is on foreground. When the task expirationHandler is called, do i must finish my background work before calling task.setTaskCompleted(success: false)? I would like to have the background work suspended not cancelled. So when i reschedule another background processing task later or the app moves to foreground the same background task will continue. Is it ok practice? Will iOS might kill the background process (and the app) if not finished work before calling task.setTaskCompleted()? Or can i trust it will only get suspended and be able to resume later? Kind Regards
1
0
354
Oct ’23
CloudKit and Background tasks
Hi. Didn't find the answer in the documentation. Are CloudKit procedures executables in background tasks ? I'm trying to do : "[privateDatabase fetchRecordWithID:myMasterRecordID completionHandler:^(CKRecord *myMasterRecord, NSError *error) { ... }]" but it returns a record only when the app is active. In background task, it does nothing and waits until app is active. Thanks for you answer. Best Claude
1
0
678
Oct ’23
Send Notification when Ble Device is discovered.
Hello there, So I'm currently building a small Ble Device that can be controlled via an app. For ease of use I want to give the users the ability to turn on a setting to receive a notification when the the device is turned on or in range. In other words if the phone can discover it. The difficult part is that I want this behaviour to also work if the app is terminated. I already tried using iBeacons by adding the iBeacon capability to my Ble Device and waiting for it to be discovered so the app launches in background mode. This technically works but I'm looking for a more elegant solution to bring the app from terminated to background if a Ble Device is scanned. Is there any way to accomplish that? Thanks for your help.
1
0
1.1k
Oct ’23
Cannot Upload HealthKitData in the Background
Hi, I am having some trouble with uploading HealthKit data to AWS S3 in the background. As of now, when I click on the button to beginBackgroundUpdates the data is uploaded as expected. When I go off the app and add data to HealthKit nothing happens. When I go back to the app, the new data is uploaded. I am not sure why this is not happening in the background. More specifically, I am not sure if this is allowed, and if so what I am doing wrong. ContentView: import SwiftUI import HealthKit struct ContentView: View { @StateObject var healthKitManager = HealthKitManager() var body: some View { VStack { Button("Enable Background Step Delivery") { healthKitManager.beginBackgroundUpdates() } } .padding() .onAppear { healthKitManager.requestAuthorization { success in print("Configured HealthKit with Return: \(success)") } } } } #Preview { ContentView() } BackgroundDeliveryApp: import SwiftUI import HealthKit import Amplify import AWSS3StoragePlugin import AWSCognitoAuthPlugin @main struct HKBackgruondDeliveryApp: App { private func configureAmplify() { do { try Amplify.add(plugin: AWSCognitoAuthPlugin()) try Amplify.add(plugin: AWSS3StoragePlugin()) try Amplify.configure() print("Succesfully configured Amplify with S3 Storage") } catch { print("Could not configure Amplify") } } init() { configureAmplify() } var body: some Scene { WindowGroup { ContentView() } } } HealthKitManager import Foundation import HealthKit import Amplify struct TestStep: Encodable, Decodable { let count: Double let startDate: Date let endDate: Date let device: String } class HealthKitManager: ObservableObject { var healthStore: HKHealthStore? let stepType = HKObjectType.quantityType(forIdentifier: .stepCount) let heartRateType = HKQuantityType(.heartRate) let sleepType = HKObjectType.categoryType(forIdentifier: .sleepAnalysis) init() { if HKHealthStore.isHealthDataAvailable() { healthStore = HKHealthStore() } else { print("There is no health data available") healthStore = nil } } func encodeStepList(stepList: [TestStep]) -> Data{ let encoder = JSONEncoder() encoder.dateEncodingStrategy = .iso8601 do { return try encoder.encode(stepList) } catch { return Data() } } func uploadStepData(stepList: [TestStep]) async { let stepData = self.encodeStepList(stepList: stepList) let uploadTask = Amplify.Storage.uploadData( key: "ExampleKey", data: stepData ) Task { for await progress in await uploadTask.progress { print("Progress: \(progress)") } } do { let value = try await uploadTask.value print("Completed: \(value)") } catch { print("Could not upload step data") } } func requestAuthorization(completion: @escaping (Bool) -> Void) { guard let stepType = stepType, let sleepType = sleepType else { return completion(false) } guard let healthStore = self.healthStore else { return completion(false) } healthStore.requestAuthorization(toShare: [], read: [stepType, heartRateType, sleepType]) { success, error in if let error = error { print("Some error has occoured during authorization of healthKit") print(error) } return completion(success) } } func beginBackgroundUpdates() { guard let healthStore = healthStore, let stepType = stepType else { print("Cannot begin background updates because HealthStore is nil") return } healthStore.enableBackgroundDelivery(for: stepType, frequency: .immediate) { success, error in print("Background update of health data") if let error = error { print("Some error has occoured during the set up of the background observer query for steps") print(error) return } guard let query = self.createObserverQuery() else { print("Could not create a query for steps") return } healthStore.execute(query) } } func stepCountDeviceRecordsQuery(stepCountObjects: @escaping ([TestStep]) -> Void) { guard let stepType = stepType else { print("Nil step type") return } let stepCountUnit = HKUnit.count() let endDate = Date() let startDate = Calendar.current.date(byAdding: .day, value: -7, to: endDate) let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate) let sortDescriptors = [NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: true)] let stepCountQuery = HKSampleQuery(sampleType: stepType, predicate: predicate, limit: 10000, sortDescriptors: sortDescriptors) { query, results, error in if let error = error { print("Error in getStepCount") print(error) return } guard let results = results else { print("Empty results in getStepCount") return } var stepCounts: [TestStep] = [] for (_, record) in results.enumerated() { guard let record: HKQuantitySample = record as? HKQuantitySample else {return} let step = TestStep(count: record.quantity.doubleValue(for: stepCountUnit), startDate: record.startDate, endDate: record.endDate, device: record.device?.model ?? "") stepCounts.append(step) } print("\(stepCounts.count) records at \(Date())") print(stepCounts[stepCounts.count - 1]) stepCountObjects(stepCounts) } healthStore?.execute(stepCountQuery) } private func createObserverQuery() -> HKQuery? { guard let stepType = stepType else { return nil } let query = HKObserverQuery(sampleType: stepType, predicate: nil) { query, completionHandler, error in self.stepCountDeviceRecordsQuery { stepList in Task { await self.uploadStepData(stepList: stepList) } } completionHandler() } return query } }
1
1
423
Oct ’23
Issue with Bluetooth Background Scanning on iOS 15.7.3. BLE. Swift
I'm facing an issue with background scanning of Bluetooth on devices with iOS 15.7.3. I'm developing an application that needs to discover BLE devices even in the background mode. However, the didDiscover method is not getting called during background scanning. Here's what I've already checked and configured in my project: Appropriate permissions for Bluetooth usage and background processing are in place. Flags in the project settings for background processing are configured correctly. I've verified that BLE devices are set up for advertising in the background. There are no physical obstacles or interference; devices and BLE devices are in an open environment. Additional details and conditions: The application initiates background scanning in the applicationDidEnterBackground method. At the beginning of the test, the BLE device is not accessible. I bring it near the iPhone after approximately 5 minutes of the app being in the background. If I don't move the BLE device away from the iPhone, it is detected almost immediately after the app goes into the background, but only once. The phone screen doesn't lock during the test. The CBCentralManagerOptionRestoreIdentifierKey option is used in the CBCentralManager. On an iPhone 12 mini with iOS 16.3.1, background scanning works, but the device is found only once for call scanForPeripherals method. The following filters are used: CBUUID(string: "330C5AD1-7261-4F06-B87C-0F6342365C2E") and CBUUID(string: "4c6607e0-2c3d-4fca-b201-0246773d6e9c"). If during the test you go to the Bluetooth settings of the iPhone (where there is a search for devices), the didDiscover method begins to report found devices Advertisement data for BLE looks like this 7 elements 0 : 2 elements key : "kCBAdvDataServiceUUIDs" value : 1 element 0 : 4C6607E0-2C3D-4FCA-B201-0246773D6E9C 1 : 2 elements key : "kCBAdvDataRxSecondaryPHY" value : 0 2 : 2 elements key : "kCBAdvDataTimestamp" value : 719149435.0168051 3 : 2 elements key : "kCBAdvDataLocalName" value : GB3_0CCE 4 : 2 elements key : "kCBAdvDataRxPrimaryPHY" value : 0 5 : 2 elements key : "kCBAdvDataServiceData" value : 1 element 0 : 2 elements key : 330C5AD1-7261-4F06-B87C-0F6342365C2E value : \<01020304\> 6 : 2 elements key : "kCBAdvDataIsConnectable" value : 1 Here is a link to the repository with the test application. This is a test application that I use for testing. The repository contains code that is not needed for testing. The required classes for testing are AppConfiguration, BackgroundScanManager, and BackgroundBLEManager. After bringing the BLE device near the iPhone, I wait for approximately 15-20 minutes. Please, help me understand why the didDiscover method is not being called in the background mode on iOS 15.7.3. Perhaps someone has encountered a similar problem and can provide advice or recommendations? Thank you very much!
1
0
802
Oct ’23
Background audio not playing on iPhone 13+
Hello everyone, I'm using Flutter and the just_audio package. When a user receives a push notification, the app plays audio in the background. I've tested this functionality on iPhone 6s and iPhone 13. It works correctly on iPhone 6s and the app plays the sound on push notification received. However on iPhone 13 the app receives the notification, starts the background process but fails to play the sound with these errors: mediaserverd(MediaExperience)[17680] &lt;Notice&gt;: -CMSUtilities- CMSUtility_IsAllowedToStartPlaying: Client sid:0x45107e5, Runner(28933), 'prim' with category MediaPlayback and mode Default and mixable does not have assertions to start mixable playback mediaserverd(MediaExperience)[17680] &lt;Notice&gt;: -CMSessionMgr- MXCoreSessionBeginInterruption_WithSecTaskAndFlags: CMSessionBeginInterruption failed as client 'sid:0x45107e5, Runner(28933), 'prim'' has insufficient privileges to take control mediaserverd(AudioSessionServer)[17680] &lt;Error&gt;: AudioSessionServerImp.mm:405 { "action":"cm_session_begin_interruption", "error":"translating CM session error", "session":{"ID":"0x45107e5","name":"Runner(28933)"}, "details":{"calling_line":879,"error_code":-16980,"error_string":"Operation denied. Cannot start playing"} } From what I understand of these errors is that on the newer iPhones, there must be additional permissions. Does anyone have any idea on how I can fix this?
1
0
814
Oct ’23
Background tasks to loaded updated products
We are building an App that collects Products from an online database. During setup of the App the initial products data set is loaded and saved into the SwiftData Database. Over time the product information will change in the online environment and we need to update this information accordingly in the App. The idea is that based on the product update date (timestamp) we will collect all the products that have been updated since the last create/update sync. Because it can be a large amount of products that need to be updated we would like to execute this update task in the background preferably during the night when the App/Device is not used. We found the following from Apple and tried to implement this, https://developer.apple.com/wwdc22/10142 We would like to use this SwiftUI background tasks feature to accommodate this but are running in some questions: Would the Backgroundtasks be the right approach to implement this feature? Can we trust that this update will be executed on the defined schedule Can we trust that this update will be completed once started?
2
0
655
Oct ’23
Connecting to a bluetooth device while app is terminated
Hello everyone! I'm currently working on an iOS app developed with Swift that involves connecting to a specific ble (Bluetooth Low Energy) device and exchanging data even when the app is terminated or running in the background. I'm trying to figure out a way to wake up my application when a specific Bluetooth device(uuid is known) is visible and then connect to it and exchange data. Is this functionality achievable? Thank you in advance for your help!
4
0
929
Oct ’23
How to Implement Lots of Tiny Uploads in the Background?
I am aiming to create a feature within an iOS application to backup all photos and videos from the device to a server, similar to Dropbox’s camera upload feature. Upon initiating the upload, I want to ensure the process continues even when the app moves to the background. Would utilizing Background Task Completion be the appropriate approach to maintain the upload process in the background? Furthermore, in scenarios where the background process gets interrupted and the app transitions back to the foreground, I want to resume the upload. What would be the suitable implementation to ensure the upload resumes seamlessly? Moreover, I have observed that an app named TeraBox continues its background processes for over 10 minutes after transitioning to the background. How might this prolonged background process be achieved?
1
0
369
Oct ’23
Background Task Only Executes Once or Twice, Not at Regular Intervals.
My background task, implemented as per Apple's documentation, runs only once or twice instead of running continuously one after another. Seeking assistance to resolve this problem. What I want to achieve I want to hit the jsonbin API continuously in regular intervals is there anything wrong in code? AppDelegate import UIKit import BackgroundTasks @main class AppDelegate: UIResponder, UIApplicationDelegate { let apiKey = "xxxxxxx" // jsonbin x-master-key let timestampUserDefaultsKey = "Timestamps" static let backgroundAppRefreshTaskSchedulerIdentifier = "com.process" func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Register a background task if #available(iOS 13.0, *) { let status = BGTaskScheduler.shared.register( forTaskWithIdentifier: AppDelegate.backgroundAppRefreshTaskSchedulerIdentifier, using: .global()) { task in self.handleBackgroundTask(task: task as! BGProcessingTask) } print(status) } return true } func handleBackgroundTask(task: BGProcessingTask) { self.scheduleAppRefresh() task.expirationHandler = { task.setTaskCompleted(success: false) self.scheduleAppRefresh() } let backgroundQueue = DispatchQueue.global(qos: .background) backgroundQueue.async { self.postToJsonBin(prefix:"P-Background") { result in switch result { case .success(let responseJSON): // Handle the success and responseJSON print("Response JSON: \(responseJSON)") task.setTaskCompleted(success: true) case .failure(let error): task.setTaskCompleted(success: false) // Handle the error print("Error: \(error.localizedDescription)") } } } } func scheduleAppRefresh() { if #available(iOS 13.0, *) { let request = BGProcessingTaskRequest(identifier: AppDelegate.backgroundAppRefreshTaskSchedulerIdentifier) // Fetch no earlier than 15 seconds from now. request.earliestBeginDate = Date(timeIntervalSinceNow: 60 * 5) // 60 seconds // request.requiresNetworkConnectivity = true do { try BGTaskScheduler.shared.submit(request) print("bg App Refresh requested") } catch { print("Could not schedule app refresh: \(error)") } } } func postToJsonBin(prefix:String, completion: @escaping (Result<Any, Error>) -> Void) { // Define the URL for jsonbin.io with the collection ID let apiUrl = URL(string: "https://api.jsonbin.io/v3/b")! // Define your JSON data including the timestamp parameter let jsonData: [String: Any] = ["timestamp": Date().currentTimestampInIST(), "prefix":prefix] do { // Serialize the JSON data let requestData = try JSONSerialization.data(withJSONObject: jsonData) // Create the URLRequest var request = URLRequest(url: apiUrl) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") request.setValue(apiKey, forHTTPHeaderField: "X-Master-Key") // Replace with your API key request.setValue(prefix, forHTTPHeaderField: "X-Bin-Name") // Set the HTTP body with the serialized JSON data request.httpBody = requestData // Create a URLSession task let task = URLSession.shared.dataTask(with: request) { (data, response, error) in if let error = error { completion(.failure(error)) return } if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 { if let responseData = data { do { // Parse the response data and call the completion handler let responseJSON = try JSONSerialization.jsonObject(with: responseData, options: []) completion(.success(responseJSON)) } catch { completion(.failure(error)) } } } else { completion(.failure(NSError(domain: "JsonBinErrorDomain", code: 0, userInfo: nil))) // Replace with appropriate error handling } } // Start the URLSession task task.resume() } catch { completion(.failure(error)) } } } extension Date { func currentTimestampInIST() -> String { let dateFormatter = DateFormatter() dateFormatter.timeZone = TimeZone(identifier: "Asia/Kolkata") // Set the time zone to IST // Define your desired date format dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" // Get the current date and time let currentDate = Date() // Format the date as a string in IST let istTimestamp = dateFormatter.string(from: currentDate) return istTimestamp } } SceneDelegate func sceneDidEnterBackground(_ scene: UIScene) { if #available(iOS 13.0, *) { let request = BGProcessingTaskRequest(identifier: AppDelegate.backgroundAppRefreshTaskSchedulerIdentifier) request.earliestBeginDate = Date(timeIntervalSinceNow: 60 * 5 ) do { try BGTaskScheduler.shared.submit(request) print("bg App Refresh requested") } catch { print("Could not schedule app refresh: \(error)") } } }
2
0
682
Oct ’23
BGProcessingTask starts only if connected to power even if requiresExternalPower is NO
I setup a BGProcessingTask that do some work with the db and at the end sends an email. I've notice that, even if I set requiresExternalPower to false the task runs only when the device is connected to the power cable. I've tested setting the repeating time every 10 minutes. If the power cable is disconnected the task isn't launhed anuymore. After I attach the cable, waiting some minutes it restarts.
7
0
2.1k
Oct ’23