Run code at a specific time regardless of app state - ios

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 :)

Related

Background tasks, notifications and startup trigger on iOS today

I hope that someone could help me to find the better solution that fit my problem.
The scenario is the following: My app should periodically connect in background to some third-party end point and fetch some data. If there is something interesting the app should present a notification, so the user can open the app and see what is interesting.
I've already done in Android with Worker API. On boot, or when the app is launched for the first time, I create a periodically work (each 30min) that connect to the end-point, download data and show a notification, if any.
On iOS what should I do? First of all, there is no bootup/startup listener. Background fetch as I understand don't suit my problem, because reboot or app kill. So I'm not able to find the right solution of my problem, the last chance is to use push notifications:
In this case I should have a server (maybe a PHP hosting with Cron Jobs) that periodically send a push notification to all my clients (maybe with firebase or iOS SDK, what's the better solution?), when the app receive that "push", should connect to the end-point, download some data, and present a user notification only if any.
Is this possible? Is there some examples? I'm sorry if the question was already asked but answers are very old and I need to know if nowadays there is a better solution to my problem.
EDIT
Thanks to anyone who participated in the comments but #Paulw11 has right. No way to be guaranteed that the app will do the background work. So, the only solution is to do everything in my backend.
Users must Sign In to the app, register on my remote database which endpoint they would follow and use Cron Job (or something like that) to send simple push notifications. I'm very disappointed.. Anyway I'm still open to other suggestions, if someone would answer to my question

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

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.

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.

ios xcode : checking live updates using a .net API

I'm trying to look for a way as to how I can notify a user of new updates based off of a .Net API that I created. Much like that notification you get on facebook, I just need to alert the user that something has been updated. So I'm thinking I need a function that runs in the background while my user uses the app combined with a NSTimer.
Based on my research (and on this question https://stackoverflow.com/a/14835300/639713) apparently you can only achieve such a thing for VOIP and location services. And that using push notification is the only way. Is it really the only option that I can use for such a need? Or are there any other ways?
Thank you for your time.
You have two options:
While your app is open, poll the server every so often to see if there are any updates to report.
This will work only as long as the user has your app open, and as you note, your app will only be allowed to stay open for a long time if it happens to be a VOIP or navigation app.
Use push notifications to push updates to the user’s device.
The disadvantage here is that you will need to write some server-side code to talk to Apple’s push notification servers (as described in this tutorial). This may or may not be a big deal in your particular situation, but it’s the only way to get data to the user when your app isn’t open.

What are the criteria for determining apps with a UIBackgroundMode acceptance?

I have a requirement to develop an app that is capable of receiving pushed information from a server - which as its not possible to intercept SMSs or apple push notifications would probably have to be implemented as a poll and see what's there or similar type of thing.
However of course such a thing isn't possible if the app isn't executing in the background.
The app couldn't be considered to be musical or voip related, however its possible that it could be considered to be gps related as the pushed information would be displayed to the user based on certain triggers, and one of those triggers could be location.
Would this app with a UIBackgroundMode of gps submitted to the app store stand a good chance of being accepted?
i have been trying to do the same thing and here is what i found
iPhone - Backgrounding to poll for events
(top answer: update 2)
shows a method that should be ok, meaning your app wont be rejected for using it. the guy who posted it said it should be ok and i have asked other members who said the same (i have not verified this myself).
here is my implementation of that post
local notifications?
You don't have to use a hack for your requirements.
iOS provides a facility whereas your application is suspended but your socket is still monitored. If there's any incoming traffic on the socket, the app is woken up and handed back the control of the socket.
Advanced App Tricks
Look under the heading, "Tips for Developing a VoIP App"

Resources