Other ways besides push to wake up an iOS app? - ios

Has anyone created an iOS app reacts to events or triggers from external sources other than push (APNS)? For example, could an iOS app subscribe to a remote topic or queue? Or listen for HTTP or socket requests?

No.
Application could be launched if:
User tapped the icon
User selected the Push-notification/Local-notification
Application supports url-schemes and is launched while opening URL
Application supports view/edit of documents and user've selected your app to do this
If anyone has other ideas - feel free to edit or add comments.
App could run in background and have active http-connection/socket. But it couldn't be opened automatically.

In addition to user action and push/local notifications, an app can be woken by a significant location change, if the app has indicated that it wants to monitor such events.
See: CLLocationManager Docs
Look for a method called startMonitoringSignificantLocationChanges. If a significant location change occurs while your app is not in the foreground or isn't running at all, your application will be launched in the background, allowing the app to perform background-only operations (e.g. no view code will run).
The app will not launch into the foreground, however, if it is launched due to a significant location change.

Related

Xamarin iOS App is not able to execute code when it is already minimized

I have a chat application, developed on the Xamarin.Forms platform, in which users can chat with each other. I have managed c# code and UI both in the shared project.
I have been facing problem since long in iOS platform. When the iOS app is running on screen, having foreground mode then the app can successfully receive a message which has been send by another user. When the app is running in the background mode and someone sends a message, I want to notify the user by using local notification (No Push remote notification - Because I think as my app is already running in minimized mode there is no need to wake up the app by implementing Push notification). Even I have implemented local notification successfully but the problem is,
When the iOS application heads to the background mode, the main thread (task) is paused so, when some user sends a message the app is not able to execute the code (when app is minimized) so that it won’t be able to show the local notification. But when the application is brought back to the foreground the thread/task get resumed and then ie shows up the local notification and also the message.
I already have selected the "Background fetch" property under Background Modes in Info.plist. I have also added below the line in my FinishedLaunching method
UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum);
I have already worked and implemented code from below links, but didn’t worked for me.
https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/
https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/ios-backgrounding-with-tasks#performing-tasks-during-didenterbackground
Xamarin forms background tasks run only when app is open on ios
https://arteksoftware.com/backgrounding-with-xamarin-forms/
When an iOS application goes to the background, are lengthy tasks paused?
How to perform a simple background task on Xamarin iOS
https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/ios-backgrounding-with-tasks#creating-background-safe-tasks
https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/
I think my issue is relate to iOS Background processing, So, does anyone has idea what to do, to execute the code when app is already in minimized mode?
Generally speaking, backgrounding is very restricted on iOS. If your app is in a certain category (e.g. Navigation, Music) you'd get extended backgrounding capabilities, but I don't believe that chat apps do. More specifically, Background Fetch is not really suitable for your problem. It is called on an irregular basis to fetch contents to be cached within your app in order to make showing contents to your users faster. Background fetch intervals may vary from 15 min to several hours (not sure about the latter).
What you need is remote notifications.
Remote notifications (also known as push notifications) let you push small amounts of data to devices on which your app is installed, even when your app isn't running.
Remote notifications are brokered via a priviliged service (Apple Push Notification service - APNs) to Apple devices and delivered in a timely manner (seconds rather than minutes or hours). Usually you'd want to keep the payload as little as possible (just send the chat ID for example) and let the app fetch its data when it's notified.
Speaking in terms of a chat application, your chat server would send the remote notification to the APNs whenever a user sends a message to the chat. The app would be notified, fetch additional data and then display the notification to the user. If the notification is tapped, the user would be taken to the chat window for the respective chat.
Please note that your app has to be registered with APNs, otherwise remote notifications won't work.

How to open the app in the background after some interval

I am building an iOS application in SWIFT where user location is fetched and sent to the server after every 30 mins. When the app is opened it keeps sending the location lat/long to the server but when the app is closed or suspended it won't send. I want to send the location even the app is closed. Is this somehow possible? I just want to send the location to server no need to open the app again and again?
You app needs to support Background Tasks for being able to run in background.
I believe this has been answered multiple times. Here is the documentation link
Since you are using location, I would recommend you to use UIBackgroundModes as locations.But be sure you don't drain the users battery.
Also usage of background fetch (UIBackgroundModes - fetch) and silent notification can be a great way to get you app running after equal interval of time.
Update
For performing operations you can use silent notification - refer this apple documentation
As per documentation - the system wakes your app in the background and calls the application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
Update 2
For local notification along with scheduling custom action refer this apple documentation. But I really doubt this will help you achieve your desired result. Mainly because you app would get an event only when the user performs any action on your local notification and if he ignores it, you app will remain in background.

Download data even when the application is terminated

Is this possible to download data when the application is terminated? I want to download data at a particular time to get new updates in my API. And if data download in the background after that if user run application again, the user has no need to wait for download new data.
If the application [server] that is serving the content has been terminated the answer is no. (There is nothing to continue writing data out to the buffer/stream)
You will need to provide more details regarding your applications concerns to determine if there is some sort of caching layer that could be implemented which would allow your downloads to be resumable in some cases.
No, you can't continue download when your app get terminated by user! Your app must require to remains in background state!!! Because if user force close app that means, he doesn't want to run it anymore. If your app is suspended by system then it can be wake up but not if it's terminated by user!
See there are two types of termination, one if user do from app switcher and another one is if iOS do. If user terminates its not possible to do any thing in background. But in case of iOS terminates your app it can continue download or background task. Check about NSURLsession background download for more info.
https://developer.apple.com/documentation/foundation/url_loading_system/downloading_files_in_the_background?language=objc
https://www.raywenderlich.com/567-urlsession-tutorial-getting-started
I agreed with other answer. You can't continue download when your app get terminated by user!
But I'm not agree with you about Background app refresh.
Background app refresh
Here is Apple's definition :
After you switch to a different app, some apps run for a short period of time before they're set to a suspended state. Apps that are in a suspended state aren’t actively in use, open, or taking up system resources. With Background App Refresh, suspended apps can check for updates and new content.
And here some parts from another doc
Apps that need to check for new content periodically can ask the system to wake them up so that they can initiate a fetch operation for that content.
...
Enabling this mode is not a guarantee that the system will give your app any time to perform background fetches.
iOS try to predict when user will launch app on next time. iOS launches the app in background to update content right before the momnent when user launch the app.
Another approach to update content is use the Push Notifications
Here is from Apple's doc
If your server sends push notifications to a user’s device when new content is available for your app, you can ask the system to run your app in the background so that it can begin downloading the new content right away.
Notification’s payload must include the content-available key with its value set to 1. This key lets iOS to know that the notification is silent.
Pushing Updates to Your App Silently.

iOS - System Alert Location use in Background

Sometimes I get an alert message, reminding me that my App is using the location service in the background.
If I read the Official Documentation, in the Give apps permission to use your Location section.
Other apps will ask for access to your location even when the app isn't in use. When you allow an app to always use your location, iOS will remind you which apps are able to use your location after an app uses your location in the background.
We can clearly admit that this alert will only be displayed when your App is using your location in the background.
So why am I asking this question ? Because this alert message just appeared when I woke up my phone, with no Apps in Background (all Apps were swiped up from the double tap Home button).
Does Background has other definition or maybe I am missing something?
Your app can be woken up (launched implicitly) by the system depending on the background modes it supports.
Even if you manually kill it in memory by swiping up, it still can be triggered in background if there is an incoming data for it to process (such as silent notification with payload).

How to listen to Calendar events when application in background iOS?

I wrote application for Android that listens on incoming Calendar events and triggers my flow when application in background.
Now I try to do the same for iOS.
I read a lot of stuff and ran some demos that use EKEventStore and background fetch.
However this approach does not satisfy me.
Is there other way to do that?
[EDIT]
Application uses public API only.
Thanks,
The short answer is you can't. Apple will not allow any app to do things like this in the background, as it requires turning the CPU on when it would otherwise be turned off.
When your app is running, you can read all events in the near future (say, two weeks out?) and setup a "local" push notification for the event time.
Also, if the user launches your app fairly often then Apple will allow your app to refresh using Background App Refresh. The device will typically do this in the morning before whatever time the user usually wakes up and turns their phone on for the first time each day, and throughout the day also, based on the user's activity. You can do anything you want during the background app refresh.
Another spanner in the works is how Apple's full disk encryption works. If the phone is locked, there really isn't much your app can do. Most data on the phone is encrypted with the user's passcode. Until the user enters their passcode not much can be done. In this case background app refresh will run as soon as they enter their passcode, but before they launch your app.
Maybe you could have a remote server send push notifications to the phone as well. Not sure if this is an option for you at all or not. Obviously this means the server would need access to the user's calendar data.
In general, the Android model of background execution doesn't apply to iOS.
You can register for calendar events as shown below, but your application will only be notified when it is running. On iOS, that means it is either in the foreground, recently backgrounded and still running, or has been launched/woken up by the system as part of a designated background mode (e.g. background fetch, location updates, VoIP, etc.).
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveEventStoreChangedNotification:)
name:EKEventStoreChangedNotification
object:eventStore];
If your application has a need for one of the designated background modes, you will at least get notifications from time to time (when you are woken up). I've seen approved apps use location updates just to get more execution time, of course YMMV.

Resources