I'm designing an app that needs to have access to an RSS feed at all times. I need to be able to access this feed in the background, while the app is not running. I already have all the code I need to access the feed and extract the info I need.
Thanks for your answer!
I don't have the exact implementation because I haven't tried it myself but Apple introduced background tasks with new SDK.
So basically you register for a background task like downloading content in your AppDelegate's
- (void)applicationWillResignActive:(UIApplication *)application
and when application becomes active you check to see if there was any new content. Again, this is a very raw explanation but hopefully should get you started.
**EDIT***
Check this out :
http://www.raywenderlich.com/29948/backgrounding-for-ios
Although this does not talk about download content in the background
Related
In a recent project I'm working on video downloading using AVAssetDownloadURLSession. I'm using AVAssetDownloadDelegate to listen to any download updates. Using the below method I'm able to get the percentage download updated for each download task.
urlSession(_:aggregateAssetDownloadTask:didLoad:totalTimeRangesLoaded:timeRangeExpectedToLoad:for:)
Issue:
The above method doesn't get called when the app is in Background. There is nothing mentioned in the Apple docs regarding the method's foreground and background behaviour.
How can I clarify how the method works? And what is the way around if I want to get the background updated as well?
I create a timer,through open the GPS keep App alive every 3 mintues,and then upload data.Then I found the timer was suspended,but the app was alive.Please someone tell me?
You need to become acquainted with Background Execution, here are few useful links I've found so far:
https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
https://blog.newrelic.com/2016/01/13/ios9-background-execution/
https://www.raywenderlich.com/143128/background-modes-tutorial-getting-started
I want to download the data from server before launch
I know there is a way to duplicate the launch view and load it till the success callback from service came
But i need to know whether there is another UIapplicationprotocol
method which can be called before didfinsishlaunch to achieve this.Can anybody guide me on this?
No. As discussed in this question, Apple requires that your application launch time be relatively short, or your app will be killed. So it's not a good idea to synchronously connect to the network before/during applicationDidFinishLaunching.
Instead, you can start an asynchronous task (see NSURLSession) and show a progress indicator/spinner while it is executing.
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions
above method will be called before method 'didfinishLaunching' method.
As far as I know,there is no such an method. but you can try to use BackgroundFetch,it can let you download data even the app is not be launched.
You can use the willFinishLaunchingWithOptions method to solve it.
It was shown in https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html?hl=ar
Encountered a very weird problem, using simple AFNetworking downloading operation, even tried with simple NSURLConnection operation, connection fails if you keep your app running, and lock screen and then unlock. Works absolutely fine in background though.
Any one encountered similar problem with NSURLConnection want to share some solution?
Thanks.
It looks like an iOS bug. Weird, but lock screen action affects NSURLSession somehow, so that it stops working and returns NSURLErrorNetworkConnectionLost. So in my app I gave up using shared session. I either use a new session object for every request or (if I need to maintain one session constantly) recreate it every time the screen gets unlocked. And it works. For users of AFNetworking or any other third party library working on top of NSURLSession the situation is harder, of course. You'll need to correct the code of the library, which is definitely not a good thing, but I think there's no other choice
Very helpful Andrey Chernukha,
In my case, figured out that you don't necessary need to recreate new session every time.
I ended up using array to save running NSURLSessionDataTasks and after phone is unlocked resume them.
Steps:
I created array NSMutableArray *dataTasksToResume
In - (void)applicationWillResignActive:(UIApplication *)application I saved all tasks to dataTasksToResume array
Cancel all running NSURLSessionDataTasks
In - (void)applicationDidBecomeActive:(UIApplication *)application get all tasks from array and resuming them (re-creating them)
Enjoy!
Hope it helps.
I have a application running in background and I need to know if device is sleeping in order to start a sincronisation process, but I didn't find information about this.
Does anyone know if it is posible and how do it?
Thanks.
You cannot know if the device is asleep because you have no control over the OS.
You can, otherwise, use the App Delegate method:
- (void)applicationWillResignActive:(UIApplication *)application
{
//your code goes here
}
if you want to wait till your app goes to background
I believe you can't do this using public API. The only thing which you can check whether your application is active or in background (using AppDelegate callbacks). And as Luke pointed out in comments, checking whether device "falls asleep" isn't iOS best design practice.
There are some private API's to do what you want, you can look at following questions:
Is there a way to check if the iOS device is locked/unlocked?
Detect screen on/off from iOS service
However, you should be aware that your app won't be accepted in AppStore in such case.