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.
Asked
Active
Viewed 95 times
0
-
please post crash details – Shubhank May 12 '16 at 08:30
-
1You could use `NSURLSessionUploadTask` to upload large file (like video files) even if your application is in background. – Larme May 12 '16 at 08:36
-
app is not getting crash when I am connected. So i am not able to find out. It works perfectly running on simulator or a connected device. – Krutika Sonawala May 12 '16 at 08:37
-
you should add a crash detection library first. answering based on assumption is not good here. – Shubhank May 12 '16 at 08:39
-
1Yes its possible. Have you tried http://code.tutsplus.com/tutorials/ios-7-sdk-background-transfer-service--mobile-20595 – Shahab Qureshi May 12 '16 at 08:41
-
@ShahabQureshi I found this tutorial really nice. Thank you so much for knowledge sharing :) – Krutika Sonawala May 12 '16 at 08:53
-
@Shubhank I will surely use crash detection library now onwards. Thank you, too :) – Krutika Sonawala May 12 '16 at 08:53
-
Got the problem from your solution is, was using synchronous uploading method, need to make it asynchronous as NSURLSession :) – Krutika Sonawala May 12 '16 at 08:59
2 Answers
1
Did you try background session? Like this:
let configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.you.uoload")
let session = NSURLSession(configuration: configuration, delegate: nil, delegateQueue: NSOperationQueue.mainQueue())

Lumialxk
- 6,239
- 6
- 24
- 47
1
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 :)

Community
- 1
- 1

Ketan Parmar
- 27,092
- 9
- 50
- 75