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
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 was wondering whether it was possible to resume a download after the app has been terminated by the user or the OS.
I can see that SKTDownloadManager has methods to pause and resume a download. However, these methods only work for current SKTDownloadObjectHelpers.
If I pull them out of SKTDownloadManager.storedDownloadObjects() after the app has been quit and then started again and throw them at SKTDownloadManager.sharedInstance().resumeDownloadForDownloadHelper() the manager won't resume the downloads.
I dug a little deeper into the code and that these downloads are not getting restarted is because of a lack of a proper status. SKTGroupedDownloadOperation.m:87:
if (self.currentRunningGroupedOperation.stateDownloadItem >= SKTMapDownloadItemStatusDownloading) {
return NO; //cannot start download,isntall, finsihed
}
So, my questions is whether it is even possible to resume downloads after the app has been terminated and if so how do I do it. Any help is appreciated :)
For resuming the download, you should trigger an event in the application:didFinishLaunching or after the application restarted:
[[SKDownloadManager sharedInstance] tryRestartDownloadsWithDelegate:self andDataSource:self];
Well you can use AFNetworking Classes for that. AFNetworkingDownloadclasses
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'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
I have searched far and wide for documentation on how to record audio in the background and have come to the conclusion that specifying 'audio' in the plist file might work. But, because iOS 4 will terminate background apps when it runs low on memory, we must also take some steps to reduce our memory usage when we transition to the background. How do we reduce our memory usage?
Also, does anybody know a sure shot way of recording audio in the background on iOS??
I unchecked the box in the Info.plist file that says "Application does not run in background" and also added the
<key> UIBackgroundModes </ key> < array> < string> audio</ string></ array>
in Info.plist. But, the recording stops as soon as I press the "HOME" button.
What callbacks do we implement to know that application has gone to background?
Please advise.
Just in case anyone else is looking for an answer here, I got mine working by adding the UIBackgroundModes array to the plist, adding 'audio' as Item 0.
I free up all memory/controllers on exit as you would by quitting the app anyhow so all that is left are the buffers the app uses (I've allocated around 1Mb though which makes me a little nervous however it seems to have worked!) I guess reducing the fidelity would help too but it seems to work as is.
In my core Audio setup I had to either change the buffer size from 1024 to 4096 or explicitly set the buffer size... I opted for the latter as latency was an issue.
NSTimeInterval iobuffersize = (float)1024.0f/SAMPLE_RATE);
sizeofdata = sizeof(iobuffersize);
AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration, &sizeofdata, &iobuffersize);
I also had to make sure that it didn't kill the app on exit via not enabling the 'Does not run in background' option however this should be off by default anyhow.
So guess I'm answering this for peace of mind for anyone else that it does work with not much setup after all.
I am however experiencing problems with Bluetooth setup, I guess that's because the buffer sizes change again but can't figure this one out... just get -50 = invalid property warning when rendering the data via the recordingCallback. I'm guessing it's the freq/sample size but who knows... will look later but seems like background now works.
Apart from specifying background recording in the plist file, we can implement applicationDidEnterBackground wihch will tell us when the application enters background. Here, we should stop any updates to the UI because that consumes memory for eg, updating a timer and an equalizer.
The call applicationWillEnterForeground will be called just before the app returns to foreground so we can resume whatever we stopped.
The recording then takes place in the background. It would also help to implement an interruption listener(this would work in the backgroud as well) so that you don't lose your recordings.