0

I am trying to figure out how to update my UIProgressView while i upload a video to my server. The video is a user picked video, here is my code to upload to my server:

NSMutableArray *array = [[NSMutableArray alloc]initWithContentsOfFile:[self saveUserLogin]];

int test;
NSString *string = [array objectAtIndex:3];
test = [string intValue];
test++;
NSData *videoData = fileData;
NSString *urlString = [[NSString alloc]initWithFormat:@"http://www.site.com/members/uploadMovie.php?&username=%@", [array objectAtIndex:0]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------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]];
NSString *postName = [[NSString alloc]initWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"vid%i.mov\"\r\n", test];
[body appendData:[[NSString stringWithString:postName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:videoData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];


NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"%@", returnString);

NSArray *values = [[NSArray alloc] initWithObjects: [array objectAtIndex:0],[array objectAtIndex:1], [array objectAtIndex:2], [NSString stringWithFormat:@"%i", test], nil];
[values writeToFile:[self saveUserLogin] atomically:YES];
[self.delegate didFinishController:self];

My UIProgressView is named ProgressForUpload, I am guessing that i'll have to use a new thread. This app i am making also uploads images, I am able to update the progressview for the images by doing this:

int copy = ForProgress;
    ForProgress = 100 / ForProgress;

    NSString *togetridof = [[NSString alloc]initWithFormat:@"%f", ForProgress];
    NSString *stringWithoutdot = [togetridof stringByReplacingOccurrencesOfString:@"." withString:@""];


    if(copy > 1 && copy < 11){
        progressString = [[NSString alloc]initWithFormat:@"0.%@", stringWithoutdot];
    }
    if (copy > 10) {
        progressString = [[NSString alloc]initWithFormat:@"0.0%@", stringWithoutdot];
    }
    if(ForProgress == 100){
        progressString = [[NSString alloc]initWithFormat:@"%@", stringWithoutdot];
    }

    tellprogress = [progressString floatValue];

Then while i upload images i create a new thread that will add tell progress onto the current progress, until its done.

I dont know if anything like this would apply to uploading a video though.

Thanks, Jacob

Vineet Singh
  • 4,009
  • 1
  • 28
  • 39
Jacob
  • 1,459
  • 2
  • 19
  • 32

1 Answers1

1

I'd implement this using asynchronous networking, then you can update the progress bar from your

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response;

Apple continues to warn that networking on the main thread is bad.

Norman
  • 453
  • 3
  • 5
  • http://www.iphonedevsdk.com/forum/iphone-sdk-development/14919-how-upload-download-http-server-2.html has some. I've ascynchronously uploaded in my app, but I don't override that delegate method since my app doesn't care about progress. (Most requests are small.) – Norman Nov 08 '11 at 19:04
  • Another one: http://mscerts.programming4.us/programming/iphone%20programming%20%20%20connecting%20to%20the%20network%20-%20getting%20data%20from%20the%20internet.aspx – Norman Nov 08 '11 at 19:04