0

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

koen
  • 5,383
  • 7
  • 50
  • 89
Khadija Daruwala
  • 1,185
  • 3
  • 25
  • 54
  • This may help: https://stackoverflow.com/questions/25372318/error-domain-nsurlerrordomain-code-1005-the-network-connection-was-lost – koen Nov 24 '20 at 13:17
  • @koen Thanks for replying, but I have already looked into it and it didn't work for me – Khadija Daruwala Nov 25 '20 at 04:24

0 Answers0