Background fetch feature allows applications to run for a short period of time (up to 30 seconds) while in the background.
The steps required to use this service are:
1. Add the fetch mode declaration:
2. Download the attached zip file and copy main.m to <project>\Files\Special.
3. Set the minimum interval (measured in seconds) with this code:
4. Handle the Application_FetchDownload event. It will be raised whenever your app is waked by this service:
5. When you completed the task you should run this code:
Note that you can run this code from any sub you like (JobDone for example).
The number (0 in the code above) is the UIBackgroundFetchResult. 0 means NewData.
The other values are listed here: https://developer.apple.com/library...ml#//apple_ref/c/tdef/UIBackgroundFetchResult
From my tests, with the minimum interval set to 0, the OS wakes the app every 10 - 15 minutes (when the screen is on).
The steps required to use this service are:
1. Add the fetch mode declaration:
B4X:
#PlistExtra: <key>UIBackgroundModes</key><array><string>fetch</string></array>
3. Set the minimum interval (measured in seconds) with this code:
B4X:
Dim no As NativeObject = App
no.RunMethod("setMinimumBackgroundFetchInterval:", Array(0)) '0 = minimum interval
4. Handle the Application_FetchDownload event. It will be raised whenever your app is waked by this service:
B4X:
Private Sub Application_FetchDownload
Log("FetchDownload")
Dim ln As Notification
ln.Initialize(DateTime.Now)
ln.AlertBody = "Fetch download..."
ln.PlaySound = True
ln.Register
End Sub
5. When you completed the task you should run this code:
B4X:
Dim no As NativeObject = App
no = no.GetField("delegate")
no.RunMethod("completeFetch:", Array(0))
The number (0 in the code above) is the UIBackgroundFetchResult. 0 means NewData.
The other values are listed here: https://developer.apple.com/library...ml#//apple_ref/c/tdef/UIBackgroundFetchResult
From my tests, with the minimum interval set to 0, the OS wakes the app every 10 - 15 minutes (when the screen is on).
B4X:
#Region Project Attributes
#ApplicationLabel: Background Fetch
#Version: 1.0.0
#iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
#iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
#PlistExtra: <key>UIBackgroundModes</key><array><string>fetch</string></array>
#End Region
Sub Process_Globals
Public App As Application
Public NavControl As NavigationController
Private Page1 As Page
End Sub
Private Sub Application_Start (Nav As NavigationController)
NavControl = Nav
Page1.Initialize("Page1")
Page1.Title = "Page 1"
Page1.RootPanel.Color = Colors.White
NavControl.ShowPage(Page1)
Dim no As NativeObject = App
no.RunMethod("setMinimumBackgroundFetchInterval:", Array(0))
End Sub
Private Sub Application_FetchDownload
Log("FetchDownload")
Dim ln As Notification
ln.Initialize(DateTime.Now)
ln.AlertBody = "Fetch download..."
ln.PlaySound = True
ln.Register
Dim no As NativeObject = App
no = no.GetField("delegate")
no.RunMethod("completeFetch:", Array(0))
End Sub
Attachments
Last edited: