-1

I have tried using NSTimer for scheduling a timely background task. However, it stopped working at some point ( Don't know what the issue was) and also it stops when user tries to intentionally kill the application.

I am required to call an API after every 1 minute irrespective of my app being in foreground and background. Similar to native iOS alarm app(kind of creating an repeating alarm).

Please any help would be appreciable. Thanks in advance.

P.S. I am new to swift development.

Iram Bukhari
  • 487
  • 1
  • 5
  • 15

2 Answers2

0

The short answer is "You can't do that." Apple does not allow 3rd party apps to run continuously in the background, with a few very specific exceptions (Navigation apps and streaming music apps are examples.)

For testing, you can set your app up as a navigation app and it will be allowed to run continuously in the background, but you'll get rejected when you try to submit it to the app store.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Thanks for the suggestion. However the app for now is proposed for individual client use. Hence, it wont be going under Apple app store review.It would be great if you can provide me with a code snippet or steps to make my app to function as navigation app. – Iram Bukhari Oct 12 '17 at 21:54
  • You need to figure out how to distribute the app. If you build it using developer credentials it will expire after a brief period (90 days if memory serves.) You might be able to distribute it using an Enterprise developer license, but that is intended for companies that develop apps for use by their employees. – Duncan C Oct 12 '17 at 23:44
  • You might be able to use an Enterprise license and treat the devices as your company's property, with the apps set up using an MDM (Mobile Device Management tool). – Duncan C Oct 12 '17 at 23:46
  • To get location updates in the background, take a look at this link: https://code.tutsplus.com/tutorials/ios-multitasking-background-location--mobile-7084 – Duncan C Oct 12 '17 at 23:49
0

You have two options: - Create a repeating Local notification that fires every minute.

func repeatNotification() {
    let content = UNMutableNotificationContent()
    content.title = "It's Time!!"
    content.body = "This is a body"
    content.categoryIdentifier = "my.reminder.category"
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60.0, repeats: true)

    let request = UNNotificationRequest(identifier: "my.reminder", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request) { (error) in
        if let error = error {
            print("error in allowing notifications: \(error.localizedDescription)")
        }
    }
    print("added notification:\(request.identifier)")
}

When the notification is fired you will get a callback in AppDelegate userNotificationCenter:willPresentNotification:withCompletionHandler:

Your second option is to register as a background location app. Basically you will ask your users to allow location all the time even when not using your app. This will allow you to run in the background indefinitely but it comes at the cost of draining battery power super fast.

If choose this option, you need to setup a location manager and ask you users to always allow location when prompted. They you will get location updates in locationManager(_:didUpdateLocations:) every X interval.

ahbou
  • 4,710
  • 23
  • 36