How can we upload background file to iphone?

like qik.com, ustream.com, I hope to learn how to download a file in the background via iPhone SDK 3.0. Pls let me know. Thank you!

+2


source to share


5 answers


It's not clear what you mean by "in the background", but if you just want you to load asynchronously, you can use NSURLConnection, NSURLRequest, or you can use this excellent library called ASIHTTPRequest . It works great and provides an easy way to show upload and download.



+2


source


You can start a new thread to download your file, look in the NSThread class, link http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/Reference/Reference.html ... you can also use the asynchronousRequest from NSURLConnection which starts a thread for you, links to http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html



0


source


You can do it this way (it's mostly cut and pasted from one of my projects). Credit goes to some position on development forums, but I don't know who it was from:

- (IBAction)startUpload:(id)sender {
    NSString *filename = [NSString stringWithFormat:@"iphone-%d.png", [NSDate timeIntervalSinceReferenceDate]];

    NSString *boundary = @"----BOUNDARY_IS_I";

    NSURL *url = [NSURL URLWithString:@"http://yourgreatwebsite.com/upload"];

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

    [req setHTTPMethod:@"POST"];

    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];

    [req setValue:contentType forHTTPHeaderField:@"Content-type"];

    NSData *imageData = UIImagePNGRepresentation([imageView image]);

    // adding the body
    NSMutableData *postBody = [NSMutableData data];


    // first parameter an image
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filename\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:imageData];

    // second parameter information
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    //[postBody appendData:[@"Content-Disposition: form-data; name=\"some_other_name\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    //[postBody appendData:[@"some_other_value" dataUsingEncoding:NSUTF8StringEncoding]];
    //[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r \n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [req setHTTPBody:postBody];

    //start the spinner and deactivate the buttons...
    [self setButtonsEnabled:NO];


    [[NSURLConnection alloc] initWithRequest:req delegate:self];
  }

  #pragma mark urlconnection delegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // this method is called when the server has determined that it
    // has enough information to create the NSURLResponse

    // it can be called multiple times, for example in the case of a
    // redirect, so each time we reset the data.
    // receivedData is declared as a method instance elsewhere
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // append the new data to the receivedData
    // receivedData is declared as a method instance elsewhere
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    // release the connection, and the data object
    [connection release];

    // inform the user
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Upload Error" message:[NSString stringWithFormat:@"The upload failed with this error: %@", [error localizedDescription]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];

    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data
    // receivedData is declared as a method instance elsewhere
#ifdef DEBUG
    NSLog(@"upload succeeded!");
#endif

    // release the connection, and the data object
    [connection release];

    NSString *response = [NSString stringWithCString:[receivedData bytes] length:[receivedData length]];    


#ifdef DEBUG
    NSLog(response);
#endif
}

      

0


source


If you are referring to loading the background when the app is not running, then you cannot (the OS does not allow it). If it works while the application is running, the links and sample code posted here work very well.

0


source


See this post for one (very thoughtful) answer.

While it is impossible to download files in the background when your application is not running, it is perfectly possible to do so while your application is running. This way you don't affect the foreground flow, and you can probably increase it to show progress, etc.

0


source







All Articles