Carregar imagem do iOS para PHP

Estou tentando fazer upload de uma imagem do meu aplicativo iOS para meu servidor da web via PHP. Aqui está o seguinte código:

-(void)uploadImage {
    NSData *imageData = UIImageJPEGRepresentation(image, 0.8);   

    //1
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];

    //2
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];


    NSString *urlString = @"http://mywebserver.com/script.php";
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"---------------------------14737809831466499882746641449"
    ;
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"iosfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];

    //3
    self.uploadTask = [defaultSession uploadTaskWithRequest:request fromData:imageData];

    //4
    self.progressBarView.hidden = NO;
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

    //5
    [uploadTask resume];
}

// update the progressbar
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.progressBarView setProgress:(double)totalBytesSent / (double)totalBytesExpectedToSend animated:YES];
    });
}

// when finished upload
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {

    dispatch_async(dispatch_get_main_queue(), ^{
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        self.progressBarView.hidden = YES;
        [self.progressBarView setProgress:0.0];
    });

    if (!error) {
        // no error
        NSLog(@"no error");
    } else {
        NSLog(@"error");
        // error
    }

}

E o seguinte trabalhando código PHP simples:

<?php
$msg = " ".var_dump($_FILES)." ";
$new_image_name = $_FILES["userfile"]["name"];
move_uploaded_file($_FILES["userfile"]["tmp_name"], getcwd() . "/pictures/" . $new_image_name);
?>

O aplicativo no iOS parece carregar a foto e a barra de progresso está funcionando, mas o arquivo não é realmente carregado quando verifico os arquivos do servidor.

Quando envio a foto com o seguinte código, ela funciona perfeitamente (EDIT: mas sem a barra de progresso):

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

E idéia de onde estou errado?

questionAnswers(1)

yourAnswerToTheQuestion