Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-13-2011, 05:25 PM   #1 (permalink)
Registered Member
 
Join Date: Oct 2011
Location: Spain
Posts: 5
DevGreg is on a distinguished road
Exclamation 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?.
DevGreg is offline   Reply With Quote
Old 10-13-2011, 05:30 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by DevGreg View Post
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

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-13-2011, 10:32 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2011
Location: London, UK
Posts: 4
MrMartin is on a distinguished road
Default

Thanks Duncan, I had same problem
MrMartin is offline   Reply With Quote
Old 10-14-2011, 07:52 AM   #4 (permalink)
Registered Member
 
Join Date: Oct 2011
Location: Spain
Posts: 5
DevGreg is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
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
DevGreg is offline   Reply With Quote
Old 10-15-2011, 01:05 PM   #5 (permalink)
Registered Member
 
Join Date: Oct 2011
Location: Spain
Posts: 5
DevGreg is on a distinguished road
Default

I'm still not able to make the download method, so if you could just tell me how to do it?
DevGreg is offline   Reply With Quote
Old 10-15-2011, 01:13 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by DevGreg View Post
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

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-15-2011, 06:49 PM   #7 (permalink)
Registered Member
 
Join Date: Oct 2011
Location: Spain
Posts: 5
DevGreg is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
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.

Code:
-(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];
}

Last edited by DevGreg; 10-15-2011 at 06:51 PM.
DevGreg is offline   Reply With Quote
Old 10-15-2011, 06:55 PM   #8 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by DevGreg View Post
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.

Code:
-(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:

Code:
        [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:

Code:
        [receivedData appendData: data]];
Depending on how big the files you're downloading, that might solve your problem.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-16-2011, 05:51 AM   #9 (permalink)
Registered Member
 
Join Date: Oct 2011
Location: Spain
Posts: 5
DevGreg is on a distinguished road
Default

OK, thanks
DevGreg is offline   Reply With Quote
Old 10-16-2011, 07:30 AM   #10 (permalink)
16 year old enthusiast
 
Join Date: Dec 2010
Location: Bratislava, Slovakia
Age: 16
Posts: 223
MacBook MH is on a distinguished road
Default

Why just not use ASIHTTPRequest? Dead easy.
MacBook MH is offline   Reply With Quote
Reply

Bookmarks

Tags
api, download, iphone, method, xcode

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 404
16 members and 388 guests
13dario13, 7twenty7, buggen, eski, EvilElf, glenn_sayers, LunarMoon, morterbaher, n00b, pbart, QuantumDoja, sacha1996, Sami Gh, UMAD, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,673
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, morterbaher
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:25 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0