Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-27864] iOS: Support for new iOS 13 background tasks

GitHub Issuen/a
TypeNew Feature
PriorityMedium
StatusOpen
ResolutionUnresolved
Affected Version/sn/a
Fix Version/sn/a
ComponentsiOS
Labels2020-Q4
ReporterJan Vennemann
AssigneeEwan Harris
Created2020-04-24T18:20:56.000+0000
Updated2021-08-20T14:35:10.000+0000

Description

With iOS 13 a new framework for handling background tasks was added: [BackgroundTasks](https://developer.apple.com/documentation/backgroundtasks) We should add support for this new framework. * Can be used to periodically launch the app in the background and do some work * Two types of tasks are available: A short refresh task and processing task that can take minutes to complete.

Comments

  1. Ewan Harris 2020-06-19

    Initial WIP PR: https://github.com/appcelerator/titanium_mobile/pull/11689
  2. Vijay Singh 2021-01-23

    PR - https://github.com/appcelerator/titanium_mobile/pull/12411
  3. Hans Knöchel 2021-01-23

    Thank you for covering this! Question: What's the difference of this API compared to the existing API? It would be great to have some docs around it. Also two questions: 1. Can this be triggered from silent push notifications? 2. What are the limits regarding network requests? An example would help a lot here. It's a superb feature!
  4. Vijay Singh 2021-01-26

    Test Case -
       const win = Ti.UI.createWindow({
           backgroundColor: '#fff'
       });
       
       const label = Ti.UI.createLabel({
           text: 'Background task',
       });
       
       win.add(label);
       win.open();
       
       Ti.App.iOS.registerBackgroundTask({
       	'identifier': 'com.test.bgrefresh',
       	'type': Ti.App.iOS.BACKGROUND_TASK_TYPE_REFRESH,
       });
       
       Ti.App.iOS.registerBackgroundTask({
       	'identifier': 'com.test.bgprocessing',
       	'type': Ti.App.iOS.BACKGROUND_TASK_TYPE_PROCESS,
       	'powerConnect': true,
       	'networkConnect': true
       });
       
       Ti.App.iOS.addEventListener('backgroundfetch', function(e) {
           Ti.API.info('backgroundfetch: ' + JSON.stringify(e));
       
           label.text = 'backgroundfetch';
       
           Ti.App.iOS.endBackgroundHandler(e.handlerId);
       });
       
       Ti.App.iOS.addEventListener('backgroundprocess', function(e) {
           Ti.API.info('backgroundprocess: ' + JSON.stringify(e));
       
           label.text = 'backgroundprocess';
        
           Ti.App.iOS.endBackgroundHandler(e.handlerId);
       });
       
    How to test - One can not test it simply as we are not sure when system will invoke the app from background. 1. Create a sample app with given test case and add keys BGTaskSchedulerPermittedIdentifiers and UIBackgroundModes with relevant values in tiapp.xml. 2. Run it on device (not simulator). 3. Open the generated Xcode project (.xcodeproj) in Xcode from build/iPhone folder. 4. Select the target, select Build Phases-> click on ' +' and select 'New Run Script Phase'. 5. Paste following script in Run Script.
       cp ${SRCROOT}/build/Products/Debug-iphoneos/${CONTENTS_FOLDER_PATH}/_index_.json ${BUILT_PRODUCTS_DIR}/${CONTENTS_FOLDER_PATH}/_index_.json
       
    6. Now run the app in device using Xcode. 7. Bring the app in background and then bring it to foreground. Then in Xcode press pause button in debugger. 8. Paste
       e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"com.test.bgrefresh"]
       
    and enter. It should change the label text. Note - For step 7 & 8 you can follow Testing section of [this](https://www.andyibanez.com/posts/modern-background-tasks-ios13/)
  5. Ewan Harris 2021-08-20

    We're removing this ticket from 10.1.0 due to the large scope and uncertainty given multiple PRs

JSON Source