Ios download file background objective c
How do I do this? I have stumbled across this post: Play music in the background using AVAudioplayer which suggests using [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; , but this only seems to allow audio to play in the background, not initiate a download. You should request the system to let you run in the background while you are downloading the audiofile.
Once you download the file, you can get the AVAudioPlayer to play your file. You need to add a UIBackgroundModes key to your info. This stops your app from being suspended and then all the normal callbacks will still run. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 9 years, 2 months ago. Active 9 years, 2 months ago. Viewed 2k times. Surely this is possible, as Pandora, Spotify, and others can do it SeongHo SeongHo 1, 2 2 gold badges 15 15 silver badges 39 39 bronze badges.
Add a comment. Active Oldest Votes. Fahim Parkar Lithu T. V Lithu T. Edited the answer. I tried it but it is not found.
What framework should I use? What is expectedBytes?? I'm sorry for asking so many questions. Saurabh Saurabh So with this I can download? Is there any time limit like nick says above? If the app takes more than 10 minutes in the background iOS will terminate your app. So if the users network is slow it won't work properly. Show 5 more comments. It is possible to start a background task when you begin the download: Apps that are transitioning to the background can request an extra amount of time to finish any important last-minute tasks.
Executing a Finite-Length Task in the Background However, such a task is limited to an undefined amount of execution time by the system. Crazyrems 2, 22 22 silver badges 37 37 bronze badges. Chris Gummer Chris Gummer 4, 1 1 gold badge 22 22 silver badges 17 17 bronze badges. You might want to look at using a network library that allows you to pause and resume downloads from where they left off, so the user doesn't have to start again from scratch if the download is interrupted.
And Background sessions let you perform uploads and downloads of content in the background while your app is not running.
Marimuthu Marimuthu 4 4 silver badges 12 12 bronze badges. This is the correct answer. The other answers pre-date this technique, but this is now the best way to accomplish long downloads that might exceed the 3 minutes allowed by UIBackgroundTask approach. Its working properly :- Please go through following steps: 1 use following line in header file of ViewController property nonatomic UIBackgroundTaskIdentifier backgroundTask; synthesis it in.
Himanshu Mahajan Himanshu Mahajan 4, 2 2 gold badges 30 30 silver badges 29 29 bronze badges. Community Bot 1 1 1 silver badge. Sumit Sharma Sumit Sharma 1, 21 21 silver badges 25 25 bronze badges. Tell the system that we are done. Standart iOS aplications: can not download data when closed. Not enough time to download mb. Then you will have privilege to run code which is inside remoteContentReady delegation. The taskIdentifier property gets its initial value -1 , while the taskResumeData property gets the nil value in case it contains any resume data.
Finally, using the main thread, the appropriate table view row is reloaded, so the label with the Ready text to be shown and the start and stop buttons to be hidden. The other delegate method we will implement, is useful in case we want to track down any error that may occurred during a download process.
As you see, we only log a message depending on whether there is an error or not. However, let me clear out that is called even when a download task is cancelled, as the cancelation is treated as an error by the method.
Feel free now to run the app, and initiate a download. Wait until it finishes, and then see the start-stop buttons going away, while the Ready label is appeared. Moreover, locate the Documents directory of the app on your hard drive, and confirm that the file has been downloaded.
Until now, we are able to initiate a file download process, to track its progress and to perform any required actions upon finishing, however we still cannot pause or stop a download task. In this part, we are going to implement these two features too, so our sample app reaches the perfection even more.
Before getting into details, let me clarify a couple of facts. First of all, the phrase pause a download task is good enough to make us understand the concept of our discussion, however programmatically speaking this is not accurate.
The truth is that either we want to pause or stop a download task, we must perform the same action, to cancel the task. Right next, it is the implementation of the pause functionality.
This time, a new download task is created by using the downloadTaskWithResumeData: method of the session object. Finally, the new task identifier is stored to the respective property. Test again the app if you want. This time you can start a download, and then pause and resume it. Well, stoping a download will seem to you pretty straightforward, after having done all the above with the download task. In short, once the appropriate FileDownloadInfo object gets located, the downloadTask property is used to cancel the download.
Note that all the other related to the downloading properties get their proper values. Finally, the table view row is reloaded to visually upgrade the results of our action. Now, you the app that can properly initiate, pause resume and stop a file download. So, give it a try. Nothing new takes place here. We have already met every single step, and now we just combine them. In any case, all or any files that are not currently being downloaded begin doing so now.
Finally, the table view is reloaded to update the interface as well. After you have added the above code in the IBAction method, you can try the app and tap or click on the Start All Downloads button.
Stoping all file downloads at once, is easier that starting them. Once again, our work almost in its entirety will take place in a loop. We will check if the isDownloading property is true, meaning that a download is in progress, and then we will cancel the respective task. Then, we will set the appropriate values to all related properties, and lastly we will reload the table view. When the app is not running and background transfers downloads or uploads are active, the system wakes the app up in case it needs any credentials or it is necessary to inform it about a complete transfer.
The second case is what we care about in this tutorial, and to be more precise, in here we will add the code that will handle all downloads that finish on the background while our app is inactive. To make it more spicy, after all transfers have finished, we will show a local notification to inform us about it. Every time that the background thread of the system the deamon that is responsible for a file download of our app has messages for it, it calls the application:handleEventsForBackgroundURLSession:completionHandler: application delegate method.
By doing so, the session identifier that woke the app up, along with a completion handler are passed to the app. The completion handler of the parameter must be stored locally, and called when all downloads are finished so the system knows that no more background activity is required all transfers are complete and any reserved resources to be freed up. Of course, upon each download finish, the URLSession:downloadTask:didFinishDownloadingToURL: delegate method is called to do all the finishing actions, such as copying the downloaded file from the temporary location to the Documents directory.
At first, go to the AppDelegate. Next, go to the AppDelegate. As you see, the completion handler passed from the system to our app is stored to the property we just declared. The backgroundTransferCompletionHandler is the one that is called when all downloads are complete. In that method we will make the call to the completion handler, and we will show the local notification.
For starters, go to the top of the ViewController. In the implementation that follows, notice that we first make sure that all downloads are over. The call to the local copy of completion handler must always take place in the main thread. Beyond than calling the completion handler, you see that we setup and present a simple local notification, without playing any sound or showing a badge number to the app icon. Of course, in a real-world app the handling code could differentiate from the above, but the important is that you are now aware of the actions you should make.
The purpose of this button is to re-initialize everything, so we can test the app without having to terminate and re-launch it. More specifically, when tapping this button, the properties of all the FileDownloadInfo objects will get their initial values, and all the downloaded files will be removed from the Documents directory. The whole job will take place in the initializeAll: IBAction method. Here is the implementation:.
Note that this feature on the sample app is totally auxiliary, and it has nothing to do with the Background Transfer Service.
0コメント