7

I am making an iOS application for myself only. I need to execute certain code every 30 minutes when application is in background.

As I am only user of this app, don’t need to worry about batter percentage and apple review process. I can use any/all background modes VOIP, music, etc.

Is is possible to run that code in background every 30 minutes? Kindly guide me the direction.

Bhushan B
  • 2,470
  • 4
  • 26
  • 38
  • duplicate? https://stackoverflow.com/questions/37319143/how-to-call-function-after-every-1-hourswift-background-fetch-work-when-app – Lu_ Jun 10 '19 at 07:33
  • Possible duplicate of [how to call function after every 1 hour?(Swift). Background fetch work when app is terminated?](https://stackoverflow.com/questions/37319143/how-to-call-function-after-every-1-hourswift-background-fetch-work-when-app) – Lu_ Jun 10 '19 at 07:34

2 Answers2

4

Its posible.

One way to do it is to create a fake VPN packet tunnel extension. And put your code in VPN Manager class. VPN extension part will keep running while your app is in background or even force quite by user.

You can write your code in this method

NEPacketTunnelProvider

override func startTunnelWithOptions(options: [String : NSObject]?, completionHandler: (NSError?) -> Void) {

          fetchData()
}

func fetchData() {
        // Do not use the NSTimer here that will not run in background
        let q_background = DispatchQueue.global(qos: .background)
        let delayInSeconds: Double = 300.0 // seconds
        let popTime = DispatchTime.now() +  DispatchTimeInterval.seconds(Int(delayInSeconds))
        q_background.asyncAfter(deadline: popTime) {
        // Fetch your data from server and generate local notification by using UserNotifications framework 
            fetchData()
        }
    }

Imran
  • 3,045
  • 2
  • 22
  • 33
  • thank you so much for your inputs, I will definitely try it. Basically, I want to check every 30 mins if there's any new data on the server(I don't want push notification and socket connection). If there's new data, I will give local notification. – Bhushan B Jun 10 '19 at 08:55
  • You I can fetch data from server and also generate the local notifications by using the fake VPN packet tunnel network extension. – Imran Jun 10 '19 at 09:23
  • 1
    @AmeetDhas Did this method for running code every 30 minutes work for you? I'm trying to do the same. – bigreddawg Aug 23 '19 at 22:08
  • 3
    @bigreddawg I had a working application at app store using this method. We ping server on every 5 minz. This method will keep working while device is not in Idle (sleep) state. – Imran Aug 26 '19 at 08:00
  • How to call this network extension code from main app? – Starwave Jan 06 '20 at 16:39
  • This Network extension will be up and running when the associated VPN profile is in use , which means the main app will have to load VPN profile(may be open VPN profile) to the device and start the VPN. refer simpletunnel sample provided by apple – user516542 Sep 13 '20 at 23:55
0

Why not go with Background Fetch?

It is available for apps likes News or Social media, so that apps can have the latest data even before the user interaction. It allows periodic background execution as is.

A simple call will fetch data every hour.

// Fetch data once an hour.
   UIApplication.shared.setMinimumBackgroundFetchInterval(3600)

Lastly its not a workaround or any private API. Your app will be accepted by appstore as well.

nr5
  • 4,228
  • 8
  • 42
  • 82
  • 2
    this is not very useful, since `setMinimumBackgroundFetchInterval(_:)` literally set the **MINIMUM** interval, so iOS can trigger the fetch any time after that minimum interval. For example if you set a minimum interval of 3600 seconds (one hour), the fetch can occur in one our, or two, or three hour, or even a day. And it has a lot of limits and punishments if you abuse it usage. See the [Apple documentation](https://developer.apple.com/documentation/uikit/uiapplication/1623100-setminimumbackgroundfetchinterva) – Romulo BM Jun 24 '19 at 20:42
  • 2
    Well its apple. If your app is not hungry for resources and user is actually getting something out of your app and visiting it regularly apple will allow the execution. In wwdc 2018 apple even said that if the user opens your app at let's say 8 am every day, apple will make sure it has all the resources to make sure your app has the resources. Every news app do this thing so that user can see the data just after opening the app – nr5 Jun 25 '19 at 02:13
  • You are right, but that solution works nicely only when you need some data prefetched before the app gets opened, but if you want to do other stuffs at a precisely time, or you need to fetch some data to do other background task like send local notifications, this is not very useful. Maybe the new iOS 13 API [BackgroundTasks](https://developer.apple.com/documentation/backgroundtasks) will work nicely for other needs. – Romulo BM Jun 25 '19 at 17:00
  • 2
    @RomuloBM The new `BackgroundTasks` framework is literally the same thing with a different name. – Andrew Nov 20 '19 at 17:47
  • @Andrew yep that's what I'm seeing years later. I just need something to run daily in background. – Bassel Alesh Aug 18 '23 at 12:40