I am uploading a video to the server using AFNetworking formdata but I am getting a failure response as follows
Error: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSUnderlyingError=0x281277c60 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "The network connection was lost." UserInfo={NSErrorFailingURLStringKey=https://example.com/api/,
_kCFStreamErrorDomainKey=4, NSErrorPeerAddressKey=<CFData 0x283fc45a0 [0x1fbdf7728]>{length = 16, capacity = 16, bytes = 0x100201bbcb10292a0000000000000000}, _kCFStreamErrorCodeKey=-4, NSErrorFailingURLKey=https://example.com/api/, NSLocalizedDescription=The network connection was lost.}}, NSErrorFailingURLStringKey=https://example.com/api/, NSErrorFailingURLKey=https://example.com/api/,
_kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=The network connection was lost.}
Below is the code snippet
- (AFHTTPRequestOperation *)getVideoFileUploadOperation:(Participant *)participant videoNumber:(NSInteger)videoNumber {
// add file upload operation
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"/Participant %@ - Video %ld.mp4", participant.internalIdentifier, videoNumber]];
NSDictionary *parameters = @{
@"token" : currentApiToken,
@"content" :@"file",
@"action" : @"import",
@"record" : participant.internalIdentifier,
@"field" : [NSString stringWithFormat:@"vid%ldfile", (long)videoNumber],
@"file" : [tempPath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]
};
NSURL *filePath = [NSURL fileURLWithPath:tempPath];
NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:[SettingsManager getRedCapUrlForCurrentStudy] parameters:parameters constructingBodyWithBlock:^(id <AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"file" error:nil];
} error:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^(void) {
[self stopAllUploads];
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
[participant recordingUploadedForVideo:videoNumber];
if([Participant hasVideosHaveBeenUploadedForAllParticipantsForVideo:videoNumber]) {
[ReminderManager removeRemindersForVideo:videoNumber EDD:participant.expectedDateOfDelivery];
}
[self->delegate uploadCompleteForVideo:videoNumber];
} failure:^(AFHTTPRequestOperation *operation, NSError* error) {
[self handleError:error];
}];
return operation;
}
Side note: I am using a real device to test this and not the simulator I tried a couple of solutions but none of them seem to work. Would be great if someone could please help me out here