Can i make call to webservice when App is running in background - ios

I need to make this call to web service over a period of time interval like after 10 min the application went to the background. Can I make call to web service from app when App is running on background? Is there any other way to get some latest data when app is running background?

Not possible in iOS6.x or lesser unless your application is has specific requirement to run in background like locations, Voip, music etc...
However this is possible with iOS7, please consider having a look at this tutorial on Background Fetching

You can not do this in background mode. There are few services that can be used in background.
Location updates : The app keeps users informed of their location, even while it is running in the background.
VOIP : The app provides the ability for the user to make phone calls using an Internet connection.
Audio : The app plays audible content to the user or records audio while in the background. (This content includes streaming audio or video content using AirPlay.)

Related

Background Audio mode with MPMusicPlayerController.systemMusicPlayer

I'm working on an iOS app that uses MPMusicPlayerController.systemMusicPlayer to play songs from Apple Music to the user.
My app should be able to append songs to the Music player's queue based on messages it receives from a server. I have this working with a simple WebSocket connection between the app and the server, but as soon as the app enters the background the socket is automatically closed (which makes sense).
Because the actual music playback is done by the Music app, I can't use the Background Audio background mode to keep my app alive. Is there a way around this?
Things I have already considered (and why I don't think they will work):
Remote Notifications are throttled too slow to be of any real use
Background App Refresh is also too slow
PushKit / VoIP (the app isn't a VoIP app)
Playing "blank" or nearly silent audio over the actual audio which seems too "hacky" and likely won't pass app review
Using background location tracking (again, almost certainly won't pass review)
Ditching systemMusicPlayer completely and using AVAudioPlayer with the Apple Music API (this would be reinventing the wheel a little bit and would force streaming even if the media was downloaded)
Using applicationQueuePlayer and just forcing the user to stay in app (this would be a bad user experience imo, they should be able to listen in the background)
Any help would be appreciated, thank you!
In iOS 15 and later, you should be able to just set the background plist flag for your app, and then just use ApplicationMusicPlayer.
https://developer.apple.com/documentation/musickit/applicationmusicplayer

Swift background work

I'm new to iOS development and I'm using Swift in my current project.
I'm trying to achieve something similar I did in Android. I'm trying to run a background service when the app is closed/minimised that will get executed every 2 seconds to keep the user informed that the app is still running in the background and checking for status with the server.
How can I achieve that? I've tried using the "background fetch" method but it doesn't get executed every 2 seconds.
The background service will run no longer than 5 minutes if it matters.
No you can't do that. What you can do is, call a service in applicationDidEnterBackground that lets your server know that your app is in background or killed(You will get very less time for this to get executed, approx. 3-5 sec.).
An Android app like the one you described will likely drain the device’s battery prematurely. This is the simple reason why Apple has restricted background execution to a clearly defined set of tasks:
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
This list is taken from Apple’s official description of background tasks: Background Execution.

iOS app to run continuously in background

I want my iOS apps to run continuously in background 24/7
I tried many options like background location updated with background task expiration handler, but later after some times it seems that the application gets suspended in background and user is brought back to the root view controller.
Any help will be appreciated.
For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. 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
Ap ps 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 should read this page of Apple Programming Guide : Background Execution
It is not allowed to run background tasks 24/7. But you can use many different ways.
You simply can't. Apple don't give any mean for an app to ensure background execution. iOS will give you processing time whenever it feel like it, and you cannot control that.

Keep an iOS application running in background forever

This question might be duplicate of many others but I am still asking because I really need to get any solution of it.
For iOS versions prior to 6, for devices other than those having an A7 chip.
I have an application which interacts with the Accelerometer. I am also running it in background by playing a sound file and turning the Audio services ON for background task.
Now If any third application plays music, it takes control of shared Audio Session of device which causes my background thread to call completion handler assuming that background task has been completed.
Now my application is interruptible by iOS and iOS can kill it if it remains in background for a long time after which I couldn't interact with accelerometer.
Is there any way/trick/hack to keep my application running all the time until unless I kill it by my own.
Again, this is a duplicate question but I really wanted to share my problem with you people to help me in this sense.
Apple is very clear on this:
Implementing Long-Running Background Tasks
For tasks that require more execution time to implement, you must
request specific permissions to run them in the background without
their being suspended. 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
Collecting accelerometer data is not listed. Therefore, what you are trying to do is not possible. "Ways, tricks, hacks" tend to get discovered quickly and offending apps removed from the App Store.

Implementing Long-Running Background Tasks in iOS

my client asks me to develop some app that periodically retrieves the user location & the phone battery status, and then send them to our backend server for data analysis, then feed back by push notification.
But through the app doc, I get to know that from apple ios dev doc:
For tasks that require more execution time to implement, you must
request specific permissions to run them in the background without
their being suspended. 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 keep users informed of their
location at all times, such as a navigation app
Apps that support
Voice over Internet Protocol (VoIP)
Newsstand apps that need to
download and process new content
Apps that receive regular updates
from external accessories
I'm wonder if this would be feasible if we wrap this app as some navigation app so we can have long-running background tasks? Does appstore will reject on our app?
BTW, what is the definition of navigation app by Apple?
You might consider using:
[CLLocationManager startMonitoringSignificantLocationChanges];
This will cause your app to be restarted if it has been killed whenever the location changes significantly, allowing you to update the details on the server at fairly regular intervals, assuming the user is moving. This does not require any special background permission. From the docs:
If you start this service and your application is subsequently terminated, the system automatically relaunches the application into the background if a new event arrives. In such a case, the options dictionary passed to the locationManager:didUpdateLocations: method of your application delegate contains the key UIApplicationLaunchOptionsLocationKey to indicate that your application was launched because of a location event.
Your other option is to configure the app as requiring continuous location updates in the background, but without knowing the primary function of the app it is hard to know if this will pass store submission or not.
https://github.com/yarodevuci/backgroundTask Check my code here I am using audio player that plays blank wav file Works perfectly on IOS 8 Battery usage around 10% in 24 hour period How to use:
var backgroundTask = BackgroundTask()
backgroundTask.startBackgroundTask() //Starts playing blank audio file. You can run NSTimer() or whatever you need and it will continue executing in the background.
backgroundTask.stopBackgroundTask() //Stops the task

Resources