How to do a request in background each 5 minutes in iOS - ios

I have an iOS application that communicate with an IoT gadget
And now I need a to do a request for an API each 5 minutes when the iOS app enters in background
I know this is horrible for battery consuming but in this specific case this is irrelevant since my iPhone device will always be connected to charger, and I won’t need this app in the store, so the apple politics is not a problem too
I already tried use Timer and TimerInterval, with the background state activated but none of then execute after the time I defined
Has any way to do this ?
Edit: I all my attempts I already activated background fetch, background music and all background modes

First you need to start background mode in your project. It looks like this:
To get to the background modes capability list you:
Select the project from the Project navigator.
Click the app target.
Select the Capabilities tab.
Turn the Background Modes switch on.
If you want to perform background task then select it from capability.
For more clarity for background fetch you can check below link
https://www.ioscreator.com/tutorials/background-fetch-ios-tutorial

Related

iOS - App in Background vs Local Notifcations

I'm building a project which is something like an Alarm Clock app. I know there are inherit limitations here (as compared to the built-in Apple Clock app) but I'm trying to assess if these limitations will be a blocker.
In the end, I need to be able to schedule an alarm/notification at a certain time, and have it sound when the phone is locked. Furthermore, I want to be able to interact with the app from the lock screen (including shake and volume gestures).
Does this require me having the app run in the background, and scheduling LocalNotifications? If its in the background, and the LocalNotification fires on the lock screen, is it possible for the User to interact with the application? i.e. With the app SleepCycle, you can shake the phone from the lock screen to trigger a Snooze. Is that sort of functionality possible only because the app is running in the background (in the case of that app, I know it's running in the background)
yes. you need to be running in the background for interacting on the lock screen

How to send battery updates to LE accessory from app in background when iPhone operate in peripheral mode?

I'm developing a BLE enabled App. In my App iPhone operate in peripheral mode with Bluetooth accessories. Everything works fine until I go to background Mode Because my Phone is working as peripheral so It also update accessory with battery level it doesn't work in background because app will only wakeup once accessory will send request because its already subscribe for the update I somehow have to wakeup my app in background mode and send update let's say every x minutes .
I have already enable Bluetooth LE accessory background mode.
Apparently enabling background mode only gives the 'right' to run in the background whenever there is an event triggered.
One of the method is what you already did by 'polling' from the accessories periodically by sending request to wake up the app and execute the task.
Another way is to use background refresh. The only downside of background refresh is the event will not occur at specific period. It was mentioned at about 10-15mins typically or it might tag along with other app background wake up. If the update is not very frequent or crucial, this method would probably better for your case.
You can keep your app running in background by setting the "required background modes" option in your plist. Select "App communicate with accessory".
Please see the attached image for reference

Running a back ground service in iOS8+

I have an application which needs to connect to server whenever the iPhone is active (i.e when the display screen is on). For this to work I need below 2 things.
In android we have combination of
BroadcastReceiver to get notified when the screen is active
Services to run the app process in the back ground to accomplish this task.
But in iOS I couldn't find any such service. Is there any way I can accomplish this task in iOS 8 ? Are there any equivalents for the above 2 in iOS ?
Check out this part of the documentation on downloading data in the background. You'll want to implement application:performFetchWithCompletionHandler: in your AppDelegate, turn on "Background Modes" in the Capabilities (find by clicking top item in Project navigator, the one with the blue icon next to it), and checkmark "Background fetch".
Note that the system decides when to call application:performFetchWithCompletionHandler:-- it is entirely opaque to us. It is only called when your app is not the app running in the foreground.
I don't know of any way to get a notification every time the device is unlocked.

Navigation stops after 15 min in background skobbler

I am using Skobbler sdk in my application for navigation. I need to continuously send data of navigation to other device. But the navigation in background works for only 15 minutes after that it stops navigating in background. I have also tried enabling the following key value pairs in plist :
Required Background modes-
1. App registers for location updates
2. App downloads content from the network
Please suggest some help.
Please test this in a real situation, like actually driving while using the app with navigation type being SKNavigationTypeReal.
Background apps are suspended after some time and only activated when they actually receive GPS location updates. Your app will never resume if the device is not actually moving.
This is an optimization done by Apple to improve battery life.
Some more info about background modes:
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
The next release will include an API to disable this behavior.

iOS - show consistent alert at the top of the UI when backgrounding the app (like personal hotspot does)

I am creating an alarm clock app that requires some user action within the app in order to turn the alarm off. Below is a picture of what another app, Sleep Cycle, does when you turn an alarm on and press the home screen (i.e. background the app).
Here is an image link (I can't post an image yet, no rep despite my many attempts to answer people's questions today) for the effect I want to re-create.
Those that have used iPhone's Personal Hotspot and connected a device will notice that it is the same effect, where a notification appears at the top of the UI - pushing everything down by around 20-40 points. This is highly desirable to an alarm clock app as it encourages the user to keep the app in the foreground so that the app can easily be entered when waking up (instead of relying on the 30 second sound window allowed by local notifications)
Does anyone have any ideas on how to implement this functionality. I assume that it must go somewhere in the:
- (void)applicationDidEnterBackground:(UIApplication *)application
tag within the AppDelegate, but I'm not sure what exactly I need to be reading up on. So if anyone has a link to some relevant Developer Docs that would also be extremely helpful.
Many thanks for your help,
Ryan
There are a handful of built in 'background modes' that change the status bar's appearance depending on what functionality an app provides whilst it's in the background. The one you've identified (a red status bar) is triggered when an app records audio whilst in the background. I presume Sleep Cycle must be acting as though it records audio just for this purpose. Other background modes include VoIP (which I think uses a blue status bar). Check out Apple's documentation on supporting these various background modes
In your case, you'd want to add audio to the UIBackgroundModes property in your Info.plist file.
But note that it wouldn't be unreasonable for Apple to reject an app during review if it pretends to perform one of these background tasks but doesn't. For example, there have been apps in the past that tried playing a silent audio clip continuously in order to stay awake in the background - needless to say Apple got wise to this and the app in question had to change its behaviour.

Resources