NSURLSession: background upload and then call a se

2019-03-27 04:21发布

问题:

I was trying to use the new ios7 background transfer api to upload some photos to a server. what it happened now is 1) we upload the bytes to s3 2) call a service api to 'complete' the upload

i looked at this doc and it seems background NSURLSession doesn't support 'data' task. does that mean i can't do the step 2 in background after the actual upload is done?

回答1:

If you want a simpler solution than repurposing NSURLSessionDownloadTask for your "completed" API call, you can round trip a quick http call during the callback in:

-URLSession:task:didCompleteWithError:



回答2:

For S3 with background task see my answer here

Another good resource is the apple sample code here and look for "Simple Background Transfer"

You have to create an upload task and upload from local file. While your app is running the NSURLSessionTaskDelegate delegate methods will be called on completion specifically at upload completion:

    /* 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;

If your app went to the background or even was killed by iOS (and not by the user), your app would be waken up with

URLSessionDidFinishEventsForBackgroundURLSession

And then your NSURLsession delegate method would be called

- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session

The way you should build it is as follow, in your appDelegate add

        - (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;
       }

Then in your controller/model class add

    /*
     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");
    }


回答3:

NSURLSessions has a delegate method URLSessionDidFinishEventsForBackgroundURLSession which is called after session completes all its tasks. I believe you should perform an api call out there.



回答4:

- (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");
}
}

put this code into appdelegate.m file

Try this code , hope it will help.