Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run iOS app in background forever?

Tags:

ios

swift

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.

like image 498
Ameet Dhas Avatar asked Jun 10 '19 07:06

Ameet Dhas


People also ask

Can iOS apps run in the background?

No. Applications cannot run in the background for over 10 minutes, except for a few certain situations (VOIP, playing audio, etc.) An iOS app which plays audio will keep playing audio indefinitely in the background so long as the audio is playing.

How long iOS app can run in background?

The answer is simply 600 seconds (10 minutes), reason is provided by the article above.


1 Answers

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()
        }
    }

like image 115
Imran Avatar answered Oct 06 '22 20:10

Imran