ios7后台上传和POST请求。

[英]iOS 7 background upload and POST requests


I need to upload files in the background with other HTTP POST requests before and after each upload.

每次上传之前和之后,我需要在后台上传文件和其他HTTP POST请求。

Previously I was using beginBackgroundTaskWithExpirationHandler which was working perfectly till iOS 6 but from IOS 7 it is restricted for approx 180 seconds only which is a concern.

之前我使用的是beginBackgroundTaskWithExpirationHandler,它在iOS 6之前运行得很好,但是从ios7开始,它的运行时间被限制在180秒左右,这是个问题。

I have read the documents regarding NSURLSession were in we have Background transfer service. But the problem with this is it only allows upload and download in background. It doesn't allow me to make POST request after every upload in the background.

我看了NSURLSession的文件,我们有后台转账服务。但问题是它只允许在后台上传和下载。它不允许我在后台每次上传后都进行POST请求。

So is there any way to make the POST request along with the background uploads?

那么有什么方法可以在后台上传的同时发出POST请求呢?

Any hint in the right direction would be highly appreciated.

任何正确方向的暗示都将受到高度赞赏。

3 个解决方案

#1


5  

I think you can use NSURLSessionDownloadTask to send a POST.

我想你可以用NSURLSessionDownloadTask发送一个帖子。

IMO, download task doesn't mean it is used for download. it means the response of your POST request (json/xml) will be downloaded to a local file. Then you can open that file and parse it to get the request.

在我看来,下载任务并不意味着是用来下载的。这意味着您的POST请求(json/xml)的响应将被下载到一个本地文件。然后您可以打开该文件并解析它以获取请求。

if you want you can even use a NSURLSessionDownloadTask to upload files to S3. and the s3 response will be 'downloaded' to a local file..

如果需要,甚至可以使用NSURLSessionDownloadTask将文件上传到S3。s3响应将被“下载”到本地文件中。

for more information, see this question in the apple developer forum https://devforums.apple.com/thread/210515?tstart=0

有关更多信息,请参见苹果开发者论坛https://devforums.apple.com/thread/210515?

#2


3  

I successfully round-trip a vanilla http call during a background task's callback, in production code. The callback is:

在生产代码中,我成功地在后台任务回调期间来回执行一个普通的http调用。回调是:

-[id<NSURLSessionTaskDelegate> URLSession:task:didCompleteWithError:]

You get about 30 seconds there to do what you need to do. Any async calls made during that time must be bracketed by

你有30秒的时间去做你需要做的事情。在此期间进行的任何异步调用都必须加上括号

-[UIApplication beginBackgroundTaskWithName:expirationHandler:]

and the "end task" version of that. Otherwise, iOS will kill your process when you pop the stack (while you are waiting for your async process).

还有“最终任务”的版本。否则,当您弹出堆栈时(当您等待异步进程时),iOS将杀死您的进程。

BTW, don't confuse UIApplication tasks (I call them "app tasks") and NSURLSession tasks ("session tasks").

顺便说一下,不要将UIApplication任务(我称之为“应用程序任务”)和NSURLSession任务(“会话任务”)混淆。

#3


0  

If you use uploadTaskWithRequest:fromData:completionHandler: you can make your HTTP POST request from the completion handler block:

如果您使用uploadTaskWithRequest:fromData:completionHandler:您可以从完成处理程序块中创建HTTP POST请求:

[backgroundSession uploadTaskWithRequest:request fromData:data completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    if (httpResponse.statusCode == 200) {
        NSMutableURLRequest *postRequest = [NSMutableURLRequest requestForURL:[NSURL URLWithString:@"http://somethingorother.com/"]];
        request.HTTPMethod = @"POST";
        .
        .
        .
        NSURLResponse *postResponse;
        NSError *postError;
        NSData *postResponseData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&postResponse error:&postError]; 
        // Check postResponse and postError to ensure that the POST succeeded
    }
}];

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/10/31/d343edf91ce396b3ecc1abc4485ea19c.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号