0

I would like to know how to run a scheduled method in an iOS app.

Every day, I have to send the device location to a web service twice a day. This submission must take place at 12:00 and 18:00. I used the method startMonitoringSignificantLocationChanges but it did not help me, because it has no exact time to run (as the name implies, only significant change of location). This feature should be performed automatically without user interaction with the iPhone because it is an "automatic check-in". It must be carried out with the app in any status (running or in background or terminated).

I found solutions that use the local notification, but the process should be completely transparent, without the user having to check-in, otherwise the feature would lose the concept.

How can this be done?

jscs
  • 63,694
  • 13
  • 151
  • 195
Gian
  • 1,193
  • 2
  • 8
  • 20
  • 2
    This should work with silent push messages. Without a server you are lost. – dasdom Sep 17 '15 at 13:58
  • @dasdom But when I receive a remote notification my app don't launched. And I need be transparent to the user, without any interaction. – Gian Sep 24 '15 at 13:09
  • When you receive a silent push message your app is launched and you can do stuff for a short period of time. – dasdom Sep 24 '15 at 19:02

1 Answers1

1

Dasdom is right - iOS doesn't allow you to schedule a background task. You can request the app run in the background, but cannot control when your code will actually be run (per Apple's guide, "When a good opportunity arises, the system wakes or launches your app into the background..."

Your best option is to have your service post an APNS notification to your application and handle the notification silently by posting the location back to your web service.

Scott Austin
  • 356
  • 1
  • 4
  • Thank you. But when I receive a remote notification my app don't launched. Only if user touch in notification. – Gian Sep 24 '15 at 13:08
  • Your app doesn't get launched, but the application:didReceiveRemoteNotification:fetchCompletionHandler: method *is* called, even when you're running in the background. See apple's documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveRemoteNotification:fetchCompletionHandler: – Scott Austin Nov 03 '15 at 15:52