9

我试图使用新的 ios7 后台传输 api 将一些照片上传到服务器。现在发生的事情是 1)我们将字节上传到 s3 2)调用服务 api 来“完成”上传

我查看了这个文档,似乎背景 NSURLSession 不支持“数据”任务。这是否意味着在实际上传完成后我不能在后台执行第 2 步?

4

4 回答 4

4

如果您想要一个比重新利用NSURLSessionDownloadTask“已完成”API 调用更简单的解决方案,您可以在回调期间往返快速 http 调用:

-URLSession:task:didCompleteWithError:

于 2014-01-20T18:44:31.997 回答
3

对于具有后台任务的 S3,请参阅我的答案here

另一个很好的资源是这里的苹果示例代码并寻找“简单后台传输”

您必须创建一个上传任务并从本地文件上传。当您的应用程序运行时,NSURLSessionTaskDelegate 委托方法将在完成时调用,特别是在上传完成时:

    /* Sent as the last message related to a specific task.  Error may be
    * nil, which implies that no error occurred and this task is complete. 
    */
    -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
                       didCompleteWithError:(NSError *)error;

如果您的应用程序进入后台甚至被 iOS(而不是用户)杀死,您的应用程序将被唤醒

URLSessionDidFinishEventsForBackgroundURLSession

然后将调用您的 NSURLsession 委托方法

- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session

您应该构建它的方式如下,在您的 appDelegate 添加

        - (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler
       {
           /*
            Store the completion handler. The completion handler is invoked by the view            controller's checkForAllDownloadsHavingCompleted method (if all the download tasks have been            completed).
            */
        self.backgroundSessionCompletionHandler = completionHandler;
       }

然后在您的控制器/模型类中添加

    /*
     If an application has received an -        application:handleEventsForBackgroundURLSession:completionHandler: message, the session         delegate will receive this message to indicate that all messages previously enqueued for this session have been delivered. At this time it is safe to invoke the previously stored         completion handler, or to begin any internal updates that will result in invoking the         completion handler.
     */
    - (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
    {
        APLAppDelegate *appDelegate = (APLAppDelegate *)[[UIApplication sharedApplication] delegate];
        if (appDelegate.backgroundSessionCompletionHandler) {
            void (^completionHandler)() = appDelegate.backgroundSessionCompletionHandler;
            appDelegate.backgroundSessionCompletionHandler = nil;
            completionHandler();
        }

        NSLog(@"All tasks are finished");
    }
于 2013-10-31T04:21:57.537 回答
0

NSURLSessions有一个委托方法URLSessionDidFinishEventsForBackgroundURLSession,在会话完成所有任务后调用。我相信你应该在那里执行一个 api 调用。

于 2013-10-17T07:07:44.303 回答
-2
- (void)applicationDidEnterBackground:(UIApplication *)application
{

if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
{
    NSLog(@"Multitasking Supported");

    __block UIBackgroundTaskIdentifier background_task;
    background_task = [application beginBackgroundTaskWithExpirationHandler:^ {

        //Clean up code. Tell the system that we are done.
        [application endBackgroundTask: background_task];
        background_task = UIBackgroundTaskInvalid;
    }];

    //To make the code block asynchronous
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        //### background task starts
        NSLog(@"Running in the background\n");
        while(TRUE)
        {
            NSLog(@"Background time Remaining: %f",[[UIApplication sharedApplication] backgroundTimeRemaining]);
            [NSThread sleepForTimeInterval:1]; //wait for 1 sec
        }
        //#### background task ends

        //Clean up code. Tell the system that we are done.
        [application endBackgroundTask: background_task];
        background_task = UIBackgroundTaskInvalid; 
    });
}
else
{
    NSLog(@"Multitasking Not Supported");
}
}

将此代码放入 appdelegate.m 文件中

试试这个代码,希望它会有所帮助。

于 2013-12-06T11:03:18.790 回答