iPhone Developer Help
Tutorials & Sample Code
Tutorials & Sample Code
Oct 14th
First you need to open Xcode and start a new iphone project and pick View-based Application
Now call it Downloader or what ever you want.
In the DownloaderViewController.h you will need to enter this.
In the DownloaderViewController.m you will need to enter all this.
-(IBAction)downloadButton {
[Home loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://iphonedeveloperhelp.info/AppleLogo.png"]]];
[Home setDelegate:self];
alertBool = YES;
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"shouldStartLoadWithRequest");
if (alertBool == YES) {
NSURL *url = request.URL;
//urlString = [[NSString alloc] init];
self.urlString = url.absoluteString;
self.nameString = url.absoluteString.lastPathComponent;
if ([urlString hasSuffix:@".png"])
{
[Home stopLoading];
NSLog(@"Supported file type found");
dialog = [[UIAlertView alloc] initWithTitle:@"Options" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Download", nil];
[dialog show];
[dialog release];
}
else {
}
}
else {
alertBool = YES;
}
return YES;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != [alertView cancelButtonIndex]){
if (buttonIndex == 0) {
//Cancel
}
else if (buttonIndex == 1) {
// Download Episode
alertBool = NO;
[dialog dismissWithClickedButtonIndex:0 animated:YES];
[self saveFileAtURL:[NSURL URLWithString:urlString]];
self.navigationItem.hidesBackButton = YES;
}
}
}
- (void)saveFileAtURL:(NSURL *)theUrl {
NSURLRequest *theRequest = [NSURLRequest requestWithURL:theUrl];
NSFileManager *manager = [NSFileManager defaultManager];
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
filename = [[NSString alloc] initWithFormat:@"%@/%@", docDir, nameString];
[manager createFileAtPath:filename contents:nil attributes:nil];
file1 = [[NSFileHandle fileHandleForWritingAtPath:filename] retain];
// create the connection with the request and start loading the data
NSURLConnection *theConnection = [[[NSURLConnection alloc] initWithRequest:theRequest delegate:self] retain];
if (theConnection)
{
NSMutableData *receivedData = [[NSMutableData data] retain];
self.theData = receivedData;
[receivedData release];
}
else
{
// inform the user that the download could not be made
NSLog(@"Error");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)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
[self.theData setLength:0];
NSLog(@"Server answered");
downloadCheckTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(downloadCheck) userInfo:nil repeats:YES];
downloadLength = [[[response allHeaderFields] objectForKey:@"Content-Length"] intValue];
}
#define ONE_MB 1024
#define TWO_AND_A_HALF_MB 1*ONE_MB
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
Message.text = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@", nameString]];
[self.theData appendData:data];
if(theData.length > TWO_AND_A_HALF_MB && file1!=nil)
{
[file1 writeData:self.theData];
self.theData = [NSMutableData data];
}
downloadedLength += [data length];
[progressViews setProgress:(float)downloadedLength/(float)downloadLength];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// release the connection, and the data object
[connection release];
// receivedData is declared as a method instance elsewhere
[self.theData release];
[file1 closeFile];
// inform the user
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
//NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
//int ran = random() % 1000000;
Message.text = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@", nameString]];
file1 = [NSFileHandle fileHandleForUpdatingAtPath:filename];
[file1 closeFile];
// release the connection, and the data object
//[receivedData release];
// Auto dismiss after 3 seconds
[self performSelector:@selector(performDismiss) withObject:nil afterDelay:3.0f];
[connection release];
}
- (void) performDismiss {
[progressViews setProgress:0.0];
downloadLength = 0;
downloadedLength = 0;
Messages.text = @"0KB of 0KB";
Message.text = @"Saved!";
self.navigationItem.hidesBackButton = NO;
}
- (NSString *)stringWithBytes:(int)theBytes {
double bytes = (double)theBytes;
NSString *type = @"Bytes";
if (bytes>1024.00) {
type = @"KB";
bytes = bytes/1024.00;
if (bytes>1024.00) {
type = @"MB";
bytes = bytes/1024.00;
if (bytes>1024.00) {
type = @"GB";
bytes = bytes/1024.00;
}
}
}
return [NSString stringWithFormat:@"%.2f %@", bytes, type];
}
- (NSString *)stringWithSeconds:(int)time {
int seconds = time%60;
time = time/60;
int minutes = time%60;
time = time/60;
int hours = time%24;
int days = time/24;
NSString *string;
if (days!=0) {
string = [NSString stringWithFormat:@"%d:%02d:%02d:%02d", days, hours, minutes, seconds];
} else if (hours!=0) {
string = [NSString stringWithFormat:@"%d:%02d:%02d", hours, minutes, seconds];
} else {
string = [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];
}
return string;
}
- (void)downloadCheck {
[Messages setText:[NSString stringWithFormat:@"%@ of %@", [self stringWithBytes:downloadedLength], [self stringWithBytes:downloadLength]]];
}
- (void)dealloc {
[super dealloc];
}
@end
Setting up the view.
Connect Home the the UIWebView
Connect Message to the UILable “Something Downloading”
Connect Messages to the UILable “0Bytes of 0Bytes”
Connect progressViews to the UIProgressView
Connect downloadButton to the UIButton and set it to Touch Up Inside
How to get the downloaded contents from the app to your computer?
In the Info.plist add this.
![]()
Now you can get all the files you download on to your computer through iTunes.
I hope this tutorial helped you.
Download Source Code
http://www.megaupload.com/?d=X4C7ARB8