NSURLSession: background upload and then call a service api

后端 未结 4 2130
我在风中等你
我在风中等你 2021-02-15 01:33

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 \'compl

4条回答
  •  萌比男神i
    2021-02-15 02:34

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

提交回复
热议问题