在iOS应用程序处于后台时上传视频文件?

时间:2023-02-09 01:28:47

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

我目前正在开发一个iOS应用程序,它涉及录制和上传视频(从内置摄像头)到服务器。

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.

目前,您录制视频,将其作为MP4存储在文件系统上,后台线程将该文件上载到服务器。这一切都发生在应用程序打开时,最终会出现一个屏幕,该屏幕基本上会告诉您等到该过程完成。

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.

如果您按下主页按钮以“最小化”应用程序(我不熟悉iOS术语,请原谅我),目前所有上传过程都已暂停。客户希望拥有它,以便您可以在此过程继续时最小化并执行完全不同的操作,并在上载完成后显示通知。

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

我的理解是iOS提供了一些下载,流媒体音乐和位置信息的特殊情况。

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.

据说曾经有一段时间你可以获得十分钟左右的背景时间,同时你的应用程序被最小化以完成任务 - 之后iOS会强制暂停一切,直到应用程序处于正面并再次激活。这显然已经在iOS的新版本中发生了变化,这意味着你不能再依赖于一个特定的数字了 - 但无论如何十分钟都不够好。

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?

我可以想到滥用上述功能的hacky方式,但我半信半疑苹果可能会在iTunes提交过程中发现这一点。我真的在寻找一种更干净的方法 - 如何在应用程序最小化的同时继续上传视频?

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

我假设有一个解决方案 - Dropbox可以处理这种情况吗?

1 个解决方案

#1


13  

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.

令人惊讶的是,我对此有所了解,尽管有相当多的指南表明这几乎是不可能的,甚至Dropbox承认它必须做一个基于位置的hacky事情。

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.

代码使用NSURLSession类,并且为了上传,您使用其uploadTaskWithStreamedRequest()方法,传递HTTP请求并获得NSURLSessionUploadTask实例作为回报。

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.

然而,我没有立即明白的是,“恢复”该任务导致文件被独立地从应用程序的其余部分上传,即当应用程序被最小化时,此任务继续,直到它完成或iOS强制它暂停。

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.

从某种意义上说,我已经实现了我的要求而没有意识到,但是这个任务仍然可以在几秒钟后被iOS中断。应用程序的其余部分也会暂停,因此通信会受到阻碍,直到应用程序再次返回到前端。

The trick is these two methods:

诀窍是这两种方法:

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

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

and

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

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.

有了这些,“begin”函数之后的任何代码都将运行,无论应用程序是否被最小化,并且将一直执行,直到调用“end”函数。通过一些调整,我已经能够按顺序上传文件并在完成该过程后发布通知。

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

我没有看到这个解决方案被暗示,所以这可能是一个坏主意,但它似乎有效。

#1


13  

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.

令人惊讶的是,我对此有所了解,尽管有相当多的指南表明这几乎是不可能的,甚至Dropbox承认它必须做一个基于位置的hacky事情。

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.

代码使用NSURLSession类,并且为了上传,您使用其uploadTaskWithStreamedRequest()方法,传递HTTP请求并获得NSURLSessionUploadTask实例作为回报。

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.

然而,我没有立即明白的是,“恢复”该任务导致文件被独立地从应用程序的其余部分上传,即当应用程序被最小化时,此任务继续,直到它完成或iOS强制它暂停。

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.

从某种意义上说,我已经实现了我的要求而没有意识到,但是这个任务仍然可以在几秒钟后被iOS中断。应用程序的其余部分也会暂停,因此通信会受到阻碍,直到应用程序再次返回到前端。

The trick is these two methods:

诀窍是这两种方法:

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

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

and

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

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.

有了这些,“begin”函数之后的任何代码都将运行,无论应用程序是否被最小化,并且将一直执行,直到调用“end”函数。通过一些调整,我已经能够按顺序上传文件并在完成该过程后发布通知。

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

我没有看到这个解决方案被暗示,所以这可能是一个坏主意,但它似乎有效。