Enable download in background with phonegap ios app - ios

I am using phonegap 2.2.0 to develop an ios app. The app requires to download some videos which i'm doing with the FileTransfer. The problem is if the app goes background i.e if the screen is locked or if the home key is pressed, download gets cancelled. I need to continue downloading the videos even when the app is in background. Is there any workaround for this?

The problem is that your app gets suspended after a short period of being in background mode, so it is no longer executing code. You need to either declare and implement your app as a service (UIBackgroundModes in Info.plist), or delay the suspension (in applicationDidEnterBackground handler). Both cases are tricky if you are a pure PhoneGap developer.
I wrote a small PhoneGap plugin for iOS that checks whether the app has called for background mode, and if so prevents it from getting suspended (normally up to 10 minutes) until the mode is revoked.
See this article for more detail.

Related

why does my phonegap app restart if kept in background for long

my phonegap app restarts if kept in background for long. For example if i open my app the next day it relaunches rather than starting from where i left off. Otherwise it seems to preserve state perfectly.
I see this happening for some other apps too that are installed on the iphone like instagram, twitter, but not for facebook and whatsapp. Does iOS kill apps if in background for long? Can i prevent this?
I am using Phonegap CLI 6.0.1 (cordova build version 4.0.1) testing on iOS9. Let me know if any other information is needed. Thanks!
iOS will kill apps when it needs the resources. There is nothing you can do to prevent your app from being killed (by iOS or the user). This has nothing to do with your app being made with phonegap (or anything else).
You need to code your app to handle being killed. When the user chooses to use your app again (after it happened to be killed), your app should launch right back to where it was, restoring its previous state. This is what all good apps do.

Background fetch is not working after killing the app

I'm trying to get data from url with background fetch. My func tries to get data and if there is a new data, it sends local notification. Background fetch works after about 10 - 20 minutes when i minimize the app. But when i kill the app (double click home button and close app) it doesn't work. I waited about 1 hour but it didn't work. I am using background service in android and it is working successfully. Is there any way to get data from url and send local notification when app is closed? I am using Xcode 6 and swift 1.2
The Background Fetching will NOT happen in your app after the user has killed it in the multitasking UI. This is by design.
Take a look at Apple's documentation that mentions:
Apps that support background execution may be relaunched by the system
to handle incoming events. If an app is terminated for any reason
other than the user force quitting it, the system launches the app...
and
In most cases, the system does not relaunch apps after they are force
quit by the user. One exception is location apps, which in iOS 8 and
later are relaunched after being force quit by the user. In other
cases, though, the user must launch the app explicitly or reboot the
device before the app can be launched automatically into the
background by the system.
As you noticed in the documentation, there's only 1 exception when the app will be relaunched if it was force killed: by using location services. Unfortunately this does not apply to you.
Also, you cannot detect if the app was force killed by the user. Check this StackOverflow answer.
The Background Fetching will NOT Update in your app after the user has killed app from background. it is not Possible

How to test Background App Launch in case of NSURLSession(Background Session) event?

How can one test the scenario of Application Launch in background for handling Background NSURLSession's event?
Flow:
Application starts a upload/download task using Background URL
session.
User hits home button. App is in suspended or in background
state.
OS decides to Exit the application. I know, one can exit the app by double-tapping home button and swipe-up the particular app. But in that case OS will never re-launch the app in background for event handling.
Upload/download task needs some event handling. OS re-lauches App in background.
So the question is how do I make OS exit the app like it may normally do after some-time. The purpose is to test the code for this scenario. I tried using UIApplicationExitsOnSuspend but it does not work since then App can not be launched in Background.
It's not a perfect solution, but I was able to manually test the launch of an app due to a Background URLSession on a physical device as follows:
Connect device for debugging via USB
Disable Internet connectivity on device (i.e. disable WiFi/Cellular)
Start the app via Xcode
Issue a request using background URLSession. The request shouldn't fail, it will just be waiting around for an Internet connection until it times out, so use a reasonably long timeout to make testing easier.
Kill the app via Xcode (press stop button)
View the device logs via Windows > Devices and Simulators
Enable Internet connectivity on the device again without starting the app
The requests from the background URLSession should then complete and in the device logs (from step 6.) you should see any NSLog statements issued as a result of the app being launched via the application(_:handleEventsForBackgroundURLSession:completionHandler:) app delegate method.
They key point is that killing the app via Xcode, as opposed to killing it using device itself, does not prevent the app from being relaunched for background event handling.
A possible alternative to killing the app manually via Xcode might be to intentionally crash the app in code - this might be more suitable for automated testing.
You could write an app that has a button that allocates and intentionally leaks chunks of memory. If you get this thing to allocate enough RAM, the OS will start killing other apps to get their RAM back.
Hopefully this would exhibit the behavior you need.

Prevent IOS to relaunch an app when I switch app to Facebook in safari

I am really stuck on this problem and I need your help! I'm doing an iPhone app.When iPhone swap between app and Facebook redirection in safari , iPhone shutdown the app for memory saving.
What I am doing is I am providing fb sharing in background from my app.
When I am doing fb sharing its shares successfully in background and comes back to my app successfully.At that time my app is not crashing. But when I go to safari and type Facebook and open fb site to check my shared post , in background my app is crashing.
This issue is coming in iPhone with iOS 7, I am not facing this issue in iOS 8.Check this post : Prevent IOS to shutdown an app when it app switch to facebook for login and permission request but this solution doesn't work for me as I am using pod to install fb and doing background share.
Though it's too late to answer this but I believe it may be helpful for others.
In App Programming Guide for iOS under "Being a Responsible Background App" section it was said that
your background running app would be terminated if it doesn't follow one of the given requirements.
You can familiarize with the full documentation. I'll quote 2 of them which exactly fit your problem:
Being a Responsible Background App
The foreground app always has precedence over background apps when it comes to the use of
system
resources and hardware. Apps running in the background need to be
prepared for this discrepancy and adjust their behavior when running
in the background. Specifically, apps moving to the background should
follow these guidelines:
Do not make any OpenGL ES calls from your code. You must not create an EAGLContext object or issue any OpenGL ES drawing commands
of any kind while running in the background. Using these calls causes
your app to be killed immediately. Apps must also ensure that any
previously submitted commands have completed before moving to the
background. For information about how to handle OpenGL ES when moving
to and from the background, see Implementing a Multitasking-aware
OpenGL ES Application in OpenGL ES Programming Guide.
Do minimal work while running in the background. The execution time given to background apps is more constrained than the amount of
time given to the foreground app. Apps that spend too much time
executing in the background can be throttled back by the system or
terminated.
To sum up, maybe your app is doing one of the above-given tasks when it's moved to the background after Facebook sharing.

What APIs does iOS call when we download an app or update from AppSTore

I am curious to understand the pipeline iOS uses when I download an App from the Store. I know packages are in .ipa based. Does iOs simply download and explode the ipa onto disk?
The iOS installer does not inform your app about installation or an update. As far as your app is concerned, nothing happens when the user installs or updates the app. It is up to you to detect any changes the first/next time your app is launched.
If your app is suspended in the background while the user chooses to update it, the OS will kill your app without notifying you (just as it can be killed in other situations, such as low memory).
If your app is executing some task in the background while the user chooses to update it (e. g. playing music, doing VoIP, or finishing a background task), I don't know what happens, but I suspect the OS will terminate your app, giving it a chance to save state via the standard APIs you already use.

Resources