Krutika Sonawala
Krutika Sonawala

Reputation: 1155

Can i keep video uploading process when my app is in background?

My iOS app is crashing when my video upload is in process and app enters to background even it did not call didEnterbackground method. Do anybody have an idea what causing it and how do I manage that uploading even if my app is in background.

Upvotes: 0

Views: 125

Answers (2)

Ketan Parmar
Ketan Parmar

Reputation: 27438

You should use NSURLSessionUploadTask to make asynchronous upload request.

Your request may be synchronous and thats why it is producing error i think.

Using AFNetworking you can do something like,

 NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
} error:nil];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
          uploadTaskWithStreamedRequest:request
          progress:^(NSProgress * _Nonnull uploadProgress) {
              // This is not called back on the main queue.
              // You are responsible for dispatching to the main queue for UI updates
              dispatch_async(dispatch_get_main_queue(), ^{
                  //Update the progress view
                  [progressView setProgress:uploadProgress.fractionCompleted];
              });
          }
          completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
              if (error) {
                  NSLog(@"Error: %@", error);
              } else {
                  NSLog(@"%@ %@", response, responseObject);
              }
          }];

  [uploadTask resume];

You can refer this answer for more details.

Hope this will help :)

Upvotes: 1

Lumialxk
Lumialxk

Reputation: 6369

Did you try background session? Like this:

let configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.you.uoload")
let session = NSURLSession(configuration: configuration, delegate: nil, delegateQueue: NSOperationQueue.mainQueue())

Upvotes: 1

Related Questions