Monday, August 15, 2011

Download Data From The Web

Designing the User Interface:- 




#import <UIKit/UIKit.h>

@interface DownloadViewController : UIViewController <UITextFieldDelegate>{
    UITextField *urlName;
    UIProgressView *progressView;
    UILabel *progressLabel;
    NSMutableData *receivedData;
    long DownloadingLength;
}

@property (nonatomic, retain) IBOutlet UILabel *progressLabel;
@property (nonatomic, retain) IBOutlet UIProgressView *progressView;
@property (nonatomic, retain) IBOutlet UITextField *urlName;


-(IBAction)startDownload;
-(void)saveData:(NSMutableData*)data toFile:(NSString*)file;

@end
The progressLabel has to show how many bytes have been downloaded and the progressView shows the progress of the download.
The user has to paste in the URL, of the data on the web, into the urlName and click on the button to download the data.
We declare one simple IBAction, that gets called, when the download finishes and a method to save the data with a given name into the documents directory.
nIn .m file 

@synthesize urlName, progressView, progressLabel;
Implementing the NSURLDelegate
Then we have to implement the IBAction we’ve declared in the header file :
-(IBAction)startDownload {
    progressLabel.text = @"0 Bytes";
    progressView.progress = 0.0f;
    NSURL *url = [[NSURL alloc] initWithString:urlName.text];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:true];
    [connection release];
    [request release];
    [url release];
    receivedData = [[NSMutableData alloc] init];
}
Ok. The next thing we have to do is to implement the needed methods from the NSURLConnection delegate :


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    if (data != nil) {
        [receivedData appendData:[[NSData alloc] initWithData:data]];  
    }
    progressLabel.text = [NSString stringWithFormat:@"%d Bytes",[receivedData length]];
    if (DownloadingLength <= 0) {
        return;
    }
        
float a = [receivedData length];
    float b = DownloadingLength;
    NSNumber *progress = [NSNumber numberWithFloat:a/b];
   
    progressView.progress = [progress floatValue];
}

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

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Download complete" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
    [alert release];
    [self saveData:receivedData toFile:@"vijay.dat"];
    [receivedData release];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    DownloadingLength = [response expectedContentLength];
}
The SaveToFile Method
Now there’s only one thing left.
The save method :
-(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];
}

No comments:

Post a Comment