Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading video files while iOS app is in background?

I'm currently working on an iOS app which involves recording and uploading videos (from the built-in camera) to a server.

It all works reasonably fine and dandy, but for a new version, the customer has requested a new feature: continuing this process without needing the app to up on the screen, and active.

At present, you record a video, it's stored as an MP4 on the file system, and a background thread uploads the file to the server. This all happens while the app is open, culminating in a screen which essentially tells you to wait until the process has finished.

If you press the home button to "minimise" the app (I'm not fluent in iOS terminology, forgive me), currently all upload processes are paused. The customer wants to have it so that you can minimise and do something entirely different as this process continues, with a notification being shown once the uploads are complete.

My understanding is that iOS offers a few special cases for downloading, streaming music and location stuff.

Supposedly once upon a time you could obtain ten minutes or so of background time while your app was minimised to finish tasks - after which time iOS would forcefully pause everything until the app was front and active again. This apparently has been changed in newer versions of iOS meaning you can't rely on a specific figure anymore - but ten minutes wasn't really good enough anyway.

I can think of hacky ways of abusing the above features but I'm half-concerned Apple might discover this during the iTunes submission process. Really I'm looking for a cleaner method - how do I continue uploading videos while the app is minimised?

I am assuming there's a solution - Dropbox can handle this situation?

like image 974
BobbyFinch Avatar asked Apr 20 '16 15:04

BobbyFinch


People also ask

Can iOS app run in background?

No. Applications cannot run in the background for over 10 minutes, except for a few certain situations (VOIP, playing audio, etc.) An iOS app which plays audio will keep playing audio indefinitely in the background so long as the audio is playing.

Will iOS terminate the app running in background after a specific time?

No, there is no specific time defined for this.

Can you play videos in the background on iPhone?

You can play YouTube videos in the background on your iPhone using YouTube Premium or Safari. YouTube Premium lets you play videos when the app isn't open, but it costs $11.99 per month. You can also play YouTube videos in the background in Safari using view mode.


1 Answers

Surprisingly I got somewhere with this, despite quite a few guides suggesting it was virtually impossible and even Dropbox admitting it has to do a hacky location-based thing.

The code was utilising the NSURLSession class, and for uploading, you use its uploadTaskWithStreamedRequest() method, passing an HTTP request and getting a NSURLSessionUploadTask instance in return.

What wasn't immediately clear to me, however, was that "resuming" that task led to files being uploaded independently from the rest of the app, i.e. when the app is minimised, this task continues until it either completes or iOS forces it to pause.

In a sense I had already achieved what I asked for without realising, however this task can still be interrupted by iOS after a few seconds. The rest of the app is also paused, so communication is hampered until the app is brought back to the front again.

The trick is these two methods:

uploadTaskID = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({})

and

UIApplication().sharedApplication().endBackgroundTask(uploadTaskID)

with these in place, any code after the "begin" function will run regardless of whether the app is minimised, and will do so until the "end" function is called. With a bit of tweaking, I've been able to get files to upload sequentially and post a notification when the process is done.

I haven't seen this solution be hinted at so it might be a bad idea, but it seemingly works.

like image 121
BobbyFinch Avatar answered Sep 28 '22 07:09

BobbyFinch