iOS Tutorial Background Fetch (Downloads)

Discussion in 'iOS Tutorials' started by Erel, Jul 9, 2015.

  1. Erel

    Erel Administrator Staff Member Licensed User

    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:
    Code:
    #PlistExtra: <key>UIBackgroundModes</key><array><string>fetch</string></array>
    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:
    Code:
    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:
    Code:
    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:
    Code:
    Dim no As NativeObject = App
       no = no.GetField(
    "delegate")
       no.RunMethod(
    "completeFetch:"Array(0))
    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).

    Code:
    #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
     

    Attached Files:

    • main.zip
      File size:
      516 bytes
      Views:
      172
    Last edited: May 3, 2016
    TWCoder, rmitev and imbault like this.
  2. rmitev

    rmitev Member Licensed User

    super :)
     
  3. fbritop

    fbritop Active Member Licensed User

    If I have

    #PlistExtra:<key>UIBackgroundModes</key><array><string>remote-notification</string></array>
    and
    #PlistExtra: <key>UIBackgroundModes</key><array><string>fetch</string></array>

    How do we combine both main.h files you have provided for Silent Push and Background Fetch?

    Thanks
    FBP
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    #PlistExtra: <key>UIBackgroundModes</key><array><string>fetch</string><string>remote-notification</string></array>
     
  5. fbritop

    fbritop Active Member Licensed User

    Thanks
    But my question was which "main.h" do I use, The one that came with silent notificacionts or the fetch one? Thay are not equaly the same file.

    I need both features in my App.

    Thanks
    FBP
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Use the attached one.
     

    Attached Files:

    fbritop likes this.
  7. susu

    susu Well-Known Member Licensed User

    How can I do Background Fetch Download for only registered users? Of course, I will check he is a registered user yet but if I add that codes my app will always fetch data, right?
     
  8. fbritop

    fbritop Active Member Licensed User

    It can be called anywhere in your applicaction. A global boolean variable should do the trick to check if you have made already the call to FetchInterval.

    Ex:

    Code:
    If fechtCall=False Then
        
    If userIsRegistered=True Then
            fechtCall=
    True
            
    Dim no As NativeObject = App
           no.RunMethod(
    "setMinimumBackgroundFetchInterval:"Array(0))
        
    End If
    End If
     
  9. susu

    susu Well-Known Member Licensed User

    Oh thank you. I thought this code to set the minimum interval like Erel wrote:

    3. Set the minimum interval (measured in seconds) with this code...
     
  10. fbritop

    fbritop Active Member Licensed User

    That's correct. The minimum you can register is 30 seconds according to iOS developer site
     
  11. susu

    susu Well-Known Member Licensed User

  12. narek adonts

    narek adonts Well-Known Member Licensed User

    What I understood here is that there is possibility to add 3rd party .m files with adding them to special folder. Like the main.m file.
    So we are not forces to create a library, when can copy the .m file and use it with NativeObject?
    Right ?
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    Not exactly. You are replacing main.m which is part of the project being sent.
     
  14. narek adonts

    narek adonts Well-Known Member Licensed User

    a ok, understood )
    but would be great to be able to add files this way.
     
  15. AHilton

    AHilton Active Member Licensed User

    Does the Simulator not support Background Fetch or am I missing something ?

    I get this error ...

    Method not found: setMinimumBackgroundFetchInterval:, target: [IsInitialized=0, DeviceUID=(null), CompanyUID=(null)

    On this code line ...

    no.RunMethod("setMinimumBackgroundFetchInterval:", Array(0)) '0 = minimum interval
     
  16. narek adonts

    narek adonts Well-Known Member Licensed User

    are you sure that no=app ?
     
  17. AHilton

    AHilton Active Member Licensed User

    Bingo! That was it, narek adonts.

    "no" was set to NativeObject = App but it needed to be TheApp, instead. App, in this project, points to another variable.

    Thanks.
     
  18. Luiz Fernando Orlandini

    Luiz Fernando Orlandini Active Member Licensed User

    This is awesome. Quick question about remote notifications...

    Is it possible to run this code when a Push is received? This means that application:didReceiveRemoteNotification:fetchCompletionHandler will be handled by B4I?

    Also, how can I get the message sent via push to take my decisions into APP code and also download any pertinent data?

    PS.: I couldn't be able to find Application_FetchDownload documented at all into core.
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    Background fetch feature has nothing to do with push notifications.

    You might be interested in this solution: Silent Push Notifications
     
  20. electro179

    electro179 Active Member Licensed User

    Hello

    When I use the Background Fetch

    The function that called is only Application_FetchDownload ?
    What is the period ?

    It is configured with

    no.RunMethod("setMinimumBackgroundFetchInterval:", Array(0))
    how much second for 0

    if I set 5, is it 5 seconds ?


    Can I received the event on the UDPSocket_PacketArrived (packet As UDPPacket) ?

    Thank you
     
Loading...