iOS - Schedule http request - ios

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.

Related

Background Fetch at Specific Time

I am looking for solution to get data in background mode even app is terminated.
There are lots of tutorials and answers available for this questions, but my questions is different than other. I haven't find any proper solution on stackoverflow, so posted this question.
I have scenario which I can explain. I'm using realm database which store event date, name, time, address etc. Now the thing is that, I want to write a code which are execute in background, In this code I want to get all event data and compare their date with today's date. And based on days remaining between these days fire local notification to remind user about how many days are remaining for specific event.
I want to call this background fetch method exactly 8 AM in local time everyday.
I haven't write any code due to confused with Background fetch and implementation. Can anyone know how to implement this ?
Help will be appreciated.
I have got solution to fix issue. As per I have talked,
I want to write a code which are execute in background, In this code I
want to get all event data and compare their date with today's date.
And based on days remaining between these days fire local notification
to remind user about how many days are remaining for specific event.
So, when I getting response from API, I'm creating local notification for all events with unique id, which are fire and repeat daily at specific time, where I have display event time and remaining days of event.
Apple documentation states that there is no guarantee when background fetch is performed. It's up to the system to decide.
The system waits until network and power conditions are good, so you should be able to retrieve adequate amounts of data quickly.
The guaranteed way to wake your app is to send at 8 am VoIP push notification from the server. It is guaranteed that the app will be wakened upon receiving a push, and you'll be able to execute the jobs you need. For more details, https://medium.com/ios-expert-series-or-interview-series/voip-push-notifications-using-ios-pushkit-5bc4a8f4d587
It is not possible to wake up the app from suspended mode in iOS except with push notification (for this server has to send push notification).
This is something which is not documented anywhere. IMHO and experience with iOS, Apple must be recording user activities since the start of the iOS era. ScreenTime is a product of those recordings only that apple was able to create and visualize the data to present a user facing app that very beautifully restricts, manages and displays your activities. In WWDC 2018, it was even quoted that apple will even detect, if the user opens your app at let's say 9 PM daily before going to bed, iOS will allow every possible resource (battery, internet, processing time etc) to that app at 9 PM. But you need to understand your user activities before you do this. Let's take an example of:
News App:
A majority of users would check the news in the morning (If they are not instagram addicts). At this time even apple should be biased to open your app with background fetch. Unless of-course there is too much of a resource crunch
A Game: Many games allow provide a restriction time by calling it "recharge" or "re-fill" the energy of a character before user can play another round. This is done so that the addicted person would buy gems to remove that restriction and hence monetize the idea. But if you know that the refill process is completed before 8:00 AM in the morning or user plays your game daily at 8:00 AM configure background fetch accordingly.
Background fetch works with interval rather than specific time.
// Fetch data once an hour.
UIApplication.shared.setMinimumBackgroundFetchInterval(3600)
Lastly why don't you try silent push notifications? Based on your question, I think silent notification is enough to wake your app after a push notification from the server, download some data and do your calculation and provide a local notification.

Real time notifications on iOS

I am developing an iOS app which needs to receive regular data from the server at specific period of time (every 5 seconds).
Apple gives developers some choices for background working.
I convinced that Background Fetch is the proper method for my problem.
But when I tested it I got confused.
Background Fetch has many issues with my requirements:
It does not fetch data at period you defined when declared it:
application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
I tried this and this:
application.setMinimumBackgroundFetchInterval(5). But no guarantee
that your app will fetch data at this interval.
When the screen is off, your app will not make any fetch until the
phone is unlocked again.
I wonder if there is a way that let my app receives real time data (or at least every 5 seconds).
Note: I read about remote notifications and I do not guarantee that I can execute it now, So I am searching for other solutions.
Thanks
There is no solution to this problem until writing this answer.
If I need a real time notification then I must use Push Notification.
There is no way to check for server data every period of time from the app only, I can send a Remote Notification when new data available on server to wake up the app and let it connect to the server.

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.

How to run an iOS application every 1 minute infinitely?

I have made an application which needs to send Location and status update to server every 1 minute.
I tried below ways but none of them helped me out. is there any solution for this?
1 - NSTimer - Many people suggested to do this way. but the problem is going to backgroundMode and it only works for 20 minutes. after that application stops sending data.
2 - BackgroundFetchMode - at beginning looks like the correct solution. But this ability do not guarantee to run application at every 1 minute. it has an algorithm that iOS decide which application should run.
This API is not like a timer task, system will decide when to call the
handler depending on many constraints.so if you set timeInterval to
2.00f(2secs), handler is called for every 2+(minimum) secs.
3 - LocationUpdate - again this way do not works because it only run your application if you move at least 500 meter.
Apps can expect a notification as soon as the device moves 500 meters
or more from its previous notification. It should not expect
notifications more frequently than once every five minutes. If the
device is able to retrieve data from the network, the location manager
is much more likely to deliver notifications in a timely manner.
4 - Push Notifications - in an article said in this method you can run an application within defined time and it really does !
you can schedule a notification within defined schedule time. but the problem is showing notification to user. which I need something silent in Background.
Also , you can not run some code every 1 minute. it just show a notification to user. and user should tap on your notification and then didReceiveLocalNotification
can be available and you can run the code.
As Apple states in their documentation:
In iOS, only specific app types are allowed to run in the background:
Apps that play audible content to the user while in the background, such as a music player app
Apps that record audio content while in the background
Apps that keep users informed of their location at all times, such as a navigation app
Apps that support Voice over Internet Protocol (VoIP)
Apps that need to download and process new content regularly
Apps that receive regular updates from external accessories
Apps that implement these services must declare the services they
support and use system frameworks to implement the relevant aspects of
those services. Declaring the services lets the system know which
services you use, but in some cases it is the system frameworks that
actually prevent your application from being suspended.
You can read all about implementing and declaring those background tasks in the link. However, if yours is another type of App, or you cannot use the system frameworks, there is no way for your App to run in the background indefinitely. And even if it is, you should always expect that the system stops your task for some reason (f.e. restarting the phone).

Get messages from server in real-time

It's generally a common question.
I wonder how do mail apps implement functionality of email-receiving?
I need to implement a simple sync between two devices(for example) using my web service.
Imagine a to do list app.
Is it possible to implement such a functionality: device1 makes an entry,then sends a message to webservice, webservice informs device2 that changes took place and device2 adjusts its data and displays that entry?
On iOS what you want could easily be implement with push notifications.
As soon as the server detects changes that device2 needs to be aware of the server will send a push notification to that device.
After the user views the notification the app should update it self, it would also be a good idea to let the app update it self when coming to the foreground.
The reason for doing it with Push Notification and not polling is that if your app is in the background you can only continue to run a process for 10 min max. You might get around this by adding the background mode to your app, like VOIP, Audio or location. But if you app does not fall in those categories apple might reject your app.
With Push notification the device will receive the notification even if your app isn't running or in background.
Basically there are 2 ways:
polling, each device asks the webserver for changes every N minutes: new todo, delete a todo, change a todo, ... and then each device will adjust. The frequency of the poll depends of the level of real time you are looking for. It can be 1 call every second or every 12 hours or much more.
implement a kind of BOSH protocol: the device opens a connection to the server. The server keeps it open until there something new to send to the device or the connection times out. In that case, the device reopens it.
Option 1 is better for your todo app because you don't need real-time accuracy. The option 2 is better for a chat application where you don't want to wait for the message.

Resources