Send device location to web service from iOS at specific time of day [duplicate] - ios

This question already has an answer here:
Call a web service from iOS at a particular time daily in the background
(1 answer)
Closed 4 years ago.
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?

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.

Related

ios closed app send location 2 times per day

I am doing a small application for iOS (with Qt and Native code)
I need send my position twice a day to a server (user REST API)
I have no ideas, I do not understand how I could launch a recovery twice a day in a service without my application being on.
Do you have any examples / suggestions to help me?
So far the solution I have found is to use the "background fetch" but I don't know if it is usable when the application is closed and not only in "background". And I also don't know if it's possible to use the locationmanager in this case. https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/updating_your_app_with_background_app_refresh
Thank you
Background fetch wouldn't work if the app is terminated by the user instead of being in a suspended state in the background.
The same goes if you are using background location updates. https://developer.apple.com/documentation/corelocation/cllocationmanager/1620568-allowsbackgroundlocationupdates
I haven't tested if this would work but you could try setting up a silent remote push notification and send it to your app twice a day, then when the app wakes to deal with the notification you get the users location.
https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app

iOS - Schedule http request

I am building a travel app which gives complete information about all transportation systems in my city. There is a requirement to notify the user when there is any change in time table of public transport/delay/any incident. To get that information, app has to hit the server at a specified time, say 9am everyday, and if anything is reported, that has to be notified to the user using local notification.
I am aware that this is not a proper design and there are many limitations with respect to Application states to fire the event. But still can iOS app manage such situation(which should work even when app is in background, suspended, inactive, terminated), as I am not getting any support from backend team?
Unfortunately if your backend team is not giving you any support then you are out of luck. Silent push is the only way to do this as per your requirement (exact time every day even if terminated). You could try the background update method, as apparently iOS learns when you're most likely to use a particular app and will schedule the update accordingly.

Run code at a specific time regardless of app state

I want to run some code everyday at 9 AM and 5 PM on iOS.
This should not be depend on application state; whatever state it has, the code must run at a given time, even in background and suspended states.
Please suggest a solution.
There are multiple ways to run the app in background and one of them which i'd believe you want is for network access, apple gives you some time to finish the request and update the UI, even if the app is closed. The user should grant access for you to do that, but is also questionable if apple will accept it if you don't do a network request and use it for something else.
Another solution would be silent push notifications, in theory the app should be awaken to execute it without asking the user anything, but you need a server and a network.
You'll want to take a look at "local notifications" in iOS. Most documentation you'll find online will talk about UILocalNotification (like for example this nice tutorial here https://www.appcoda.com/ios-programming-local-notification-tutorial/), but if you're developing an iOS 10 app you need to look at the new UNNotificationRequest class instead because UILocalNotification is deprecated in iOS 10. Have a look at the docs from Apple here: https://developer.apple.com/reference/usernotifications/unnotificationrequest
I hope this sends you in the right direction :)

iOS Enterprise app - How can I make sure my app runs in the background?

First of all it's important to note that I'm developing an Enterprise App, so there is no need for App Store guidelines \ approvals considerations.
My goal is the following:
1) An iPhone app which the user should open only one time only. During this one time he will go through some sign up process.
2) Once step (1) has finished (either by app suspension or app close), the app should "wake up" every hour (more or less) and send the server some data regarding the user from the background (all is done with the user agreement of course).
Optional Solutions I tried:
I read very thoroughly Apple guidelines for running in background in https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
I decided to go with 2 UIBackgroundModes:
location
fetch
1) For location I use:
startMonitoringSignificantLocationChanges();
manager.requestAlwaysAuthorization()
In order to make sure my app will run after "app close" or even "device reboot" I had to use both options. See Apple guidelines:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW1
If you leave the significant-change location service running and your
iOS app is subsequently suspended or terminated, the service
automatically wakes up your app when new location data arrives.
2) For background fetch I use
performFetchWithCompletionHandler()
and
UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum);
For some reason the app doesn't run in background after 24 hours. Meaning the app never "wakes up" again even though according to Apple guidelines whenever significant location update occurs - it suppose to wake up.
What can I do in order to guarantee as much as possible that my app will run regularly in the background?
In your project properties you have to allow background mode for your application to Update location and for background fetch.
Fetch in background is not so easy as it seems.
iOS will define by itself how often it will fetch data in background modeeven you set option for each 15 minutes :(
As my practice shows time interval for fetching new data is less if user open application from time to time so iOS makes some calculations about normal app use. As a result app calls background fetch data according to app ussage.
But you can make one trick:
you can call fetch procedure from you location procedure. For data exchange it was usefull in my projects :)
The solution worked with Silent PN.
My goal as noted in the bounty was: ".. to have an iOS app that runs regularly in the background for 30+ days when not opened at all. Should also run after the app closes."
I found that when sending PN with "content-available" : 1 did the trick for me.
I set up a server which sends every 15 min a "silent PN" to all registered devices. I was able to confirm from the same server that those clients receive the PN and act on it.
Thanks #andlin for the advice.

Trigger iOS app periodically

I'm trying to figure out if there is any permissible way to have my iOS app run periodically (say every 4 hours) and grab a users location. I'm aware of three possible ways this might be accomplished:
Local Notifications (Handling Local and Remote Notifications)
The problem with this solution is that when the app is killed after being in the background a while or the phone restarts, then the local notification will fire and give a UI but not actually start the app so I can't grab the location.
Alarms (Configuring Alarms)
As Apple's documentation says:
Note: An alarm is not intended to serve as a UILocalNotification. An alarm requires you to create an event or reminder that is visible in the user’s Calendar or Reminders app. A UILocalNotification is better suited for general purposes that don’t involve the Calendar database.
Therefore, not only would I be going against Apple's recommendation, but I would then have to ask for access to the user's calendar which won't make any sense to the user.
NSTimer (NSTimer)
I could create a NSTimer, but that solution was already answered here as not viable (That was three and a half years ago but I'm guessing the answer hasn't changed).
Are there any other ways in which this might be accomplished on iOS?
I don't know of any way to grab it periodically every X hours, but the Significant Change Location API might work for your use case. It gives you updates whenever there has been a significant change to the users location.
edit
If you app is not running in the background it will relaunch your app.
/edit
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW10
This stack overflow question has a lot of info about this service also:
Behaviour for significant change location API when terminated/suspended?
See the Getting Location Events in the Background section of Apple's Location and Maps Programming Guide. The other methods mentioned in the question wouldn't actually wake the app up, except for UILocalNotification, and then only when the user tapped in in Notification Center.

Resources