Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Help, need download method for iPhone.

Hello,
I want to have a download method for my iPhone, I got a download method, but when the file is bigger than 60 mb, the app crashes.
Anyone has any ideas?.
Post edited by DevGreg on

Replies

  • Duncan CDuncan C Posts: 8,024Tutorial Authors, Registered Users
    DevGreg;381539 said:
    Hello,
    I want to have a download method for my iPhone, I got a download method, but when the file is bigger than 60 mb, the app crashes.
    Anyone has any ideas?.
    Use an NSFileHandle or NSStream to write the data directly to a file as it's downloaded, rather than building it up in memory. There was a thread with sample code on this very subject a week or so ago. I suggest searching on "NSFileHandle" to find it.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • DevGregDevGreg Posts: 5New Users
    Duncan C;381540 said:
    Use an NSFileHandle or NSStream to write the data directly to a file as it's downloaded, rather than building it up in memory. There was a thread with sample code on this very subject a week or so ago. I suggest searching on "NSFileHandle" to find it.
    Thanks, I'll look for the NSFileHandle
  • DevGregDevGreg Posts: 5New Users
    I'm still not able to make the download method, so if you could just tell me how to do it?
  • Duncan CDuncan C Posts: 8,024Tutorial Authors, Registered Users
    DevGreg;381880 said:
    I'm still not able to make the download method, so if you could just tell me how to do it?
    Post your current download code that crashes when the files are too large. Also, show what you tried.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • DevGregDevGreg Posts: 5New Users
    Duncan C;381884 said:
    Post your current download code that crashes when the files are too large. Also, show what you tried.
    This is the code I've used, I guess that the problem would be in the saveData method or in the connection:didReceiveData, but I can't figure out what it can be.


    -(void)saveData:(NSMutableData *)data toFile:(NSString *)file {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true);
    NSString *temp = [paths objectAtIndex:0];
    temp = [temp stringByAppendingPathComponent:file];
    [data writeToFile:temp atomically:true];
    }

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    if (data != nil) {
    [receivedData appendData:[[NSData alloc] initWithData:data]];
    }

    progressLabel.text = [NSString stringWithFormat:@\"%d kb\",[receivedData length]/1024];
    estimatedLabel.text = [NSString stringWithFormat:@\"%d kb\", estimatedLength/1024];


    if (estimatedLength <= 0) {
    return;
    }


    float a = [receivedData length];
    float b = estimatedLength;
    NSNumber *progress = [NSNumber numberWithFloat:a/b];

    progressView.progress = [progress floatValue];

    }

    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@\"Error\" message:@\"The download failed!\" delegate:nil cancelButtonTitle:@\"Ok\" otherButtonTitles:nil];
    [alert show];
    [alert release];
    [receivedData release];
    }

    -(void)connectionDidFinishLoading:(NSURLConnection *)connection {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@\"Download Completed!\" message:@\"\n\" delegate:self cancelButtonTitle:@\"Cancel\" otherButtonTitles:@\"Enter Name\", nil];
    textField1 = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
    [textField1 setBackgroundColor:[UIColor whiteColor]];
    textField1.placeholder = @\"Name and extension (example.gif)\";
    [alert addSubview:textField1];
    [alert show];
    [alert release];


    }

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 1) {


    [self saveData:receivedData toFile:[NSString stringWithFormat:@\"%@\", textField1.text]];
    [receivedData release];

    }

    }

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    estimatedLength = [response expectedContentLength];
    }

    -(BOOL)textFieldShouldReturn:(UITextField *)textField {

    progressLabel.text = @\"0 kb\";
    estimatedLabel.text = @\"0 kb\";
    progressView.progress = 0.0f;
    NSURL *url = [[NSURL alloc] initWithString:urlField.text];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:true];
    [urlField resignFirstResponder];
    [connection release];
    [request release];
    [url release];
    receivedData = [[NSMutableData alloc] init];

    }

    -(IBAction)startDownload {
    progressLabel.text = @\"0 kb\";
    estimatedLabel.text = @\"0 kb\";
    progressView.progress = 0.0f;
    NSURL *url = [[NSURL alloc] initWithString:urlField.text];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:true];
    [urlField resignFirstResponder];
    [connection release];
    [request release];
    [url release];
    receivedData = [[NSMutableData alloc] init];
    }
  • Duncan CDuncan C Posts: 8,024Tutorial Authors, Registered Users
    DevGreg;381933 said:
    This is the code I've used, I guess that the problem would be in the saveData method or in the NSURLConnection, but I can't figure out what it can be.


    -(void)saveData:(NSMutableData *)data toFile:(NSString *)file {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true);
    NSString *temp = [paths objectAtIndex:0];
    temp = [temp stringByAppendingPathComponent:file];
    [data writeToFile:temp atomically:true];
    }

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    if (data != nil) {
    [receivedData appendData:[[NSData alloc] initWithData:data]];
    }

    progressLabel.text = [NSString stringWithFormat:@\"%d kb\",[receivedData length]/1024];
    estimatedLabel.text = [NSString stringWithFormat:@\"%d kb\", estimatedLength/1024];


    if (estimatedLength <= 0) {
    return;
    }


    float a = [receivedData length];
    float b = estimatedLength;
    NSNumber *progress = [NSNumber numberWithFloat:a/b];

    progressView.progress = [progress floatValue];

    }

    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@\"Error\" message:@\"The download failed!\" delegate:nil cancelButtonTitle:@\"Ok\" otherButtonTitles:nil];
    [alert show];
    [alert release];
    [receivedData release];
    }

    -(void)connectionDidFinishLoading:(NSURLConnection *)connection {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@\"Download Completed!\" message:@\"\n\" delegate:self cancelButtonTitle:@\"Cancel\" otherButtonTitles:@\"Enter Name\", nil];
    textField1 = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
    [textField1 setBackgroundColor:[UIColor whiteColor]];
    textField1.placeholder = @\"Name and extension (example.gif)\";
    [alert addSubview:textField1];
    [alert show];
    [alert release];


    }

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 1) {


    [self saveData:receivedData toFile:[NSString stringWithFormat:@\"%@\", textField1.text]];
    [receivedData release];

    }

    }

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    estimatedLength = [response expectedContentLength];
    }

    -(BOOL)textFieldShouldReturn:(UITextField *)textField {

    progressLabel.text = @\"0 kb\";
    estimatedLabel.text = @\"0 kb\";
    progressView.progress = 0.0f;
    NSURL *url = [[NSURL alloc] initWithString:urlField.text];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:true];
    [urlField resignFirstResponder];
    [connection release];
    [request release];
    [url release];
    receivedData = [[NSMutableData alloc] init];

    }

    -(IBAction)startDownload {
    progressLabel.text = @\"0 kb\";
    estimatedLabel.text = @\"0 kb\";
    progressView.progress = 0.0f;
    NSURL *url = [[NSURL alloc] initWithString:urlField.text];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:true];
    [urlField resignFirstResponder];
    [connection release];
    [request release];
    [url release];
    receivedData = [[NSMutableData alloc] init];
    }

    I'll see if I can find the time to rework your code to save the data bit-by-bit to a file using an NSFileHandle.

    In the meantime, though, the code you posted has a HUGE leak. This line:

            [receivedData appendData:[[NSData alloc] initWithData:data]];    


    Creates a new copy of every piece of incoming data, and then promptly leaks it. As a result, you need twice as much free RAM as the incoming data, and leak a whole copy of the data.

    There is no reason to duplicate the incoming data. Rewrite that line as:

            [receivedData appendData: data]];    


    Depending on how big the files you're downloading, that might solve your problem.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
Sign In or Register to comment.