views:

476

answers:

2

Ok ! Coming to the point directly. What I want to do is explained as follows.

  • I have an url of MP3 file. ( for example Sound File )
  • Now, When user starts application.
  • Download should start & for that I have implemented following methods.

    -(void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url=[NSURL URLWithString:@"http://xyz.pqr.com/abc.mp3"];
    NSURLRequest *req=[NSURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:120];
    
    
    NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
    
    
    if(con){
        myWebData=[[NSMutableData data] retain];
        } else {
        //          [MainHandler performSelector:@selector(targetSelector:) withObject:nil];
      } 
     }
    

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    NSLog(@"%@",@"connection established");
    [myWebData setLength: 0];
    

    }

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSLog(@"%@",@"connection receiving data"); [myWebData appendData:data]; }

    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"%@",@"connection failed"); [connection release]; // [AlertViewHandler showAlertWithErrorMessage:@"Sorry, there is no network connection. Please check your network and try again."]; // [self parserDidEndDocument:nil]; }

    -(void)connectionDidFinishLoading:(NSURLConnection *)connection { [connection release]; }

  • Now, Above methods work perfectly for downloading. But missing points are as follows.

    • I can not get the exact size which is going to be downloaded.

( means I want to know what is the size of file - which is going to be download )

A: 

Yep ! Almost done with ASIHTTP.

-(IBAction)btnTapped:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://sound20.mp3pk.com/indian/omkara/omkara1%28songs.pk%29.mp3"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDownloadDestinationPath:[self pathForLogInCCID]];
    [myProgressIndicator setProgress:0];

    [request setDownloadProgressDelegate:myProgressIndicator];
    [request startSynchronous];
[myProgressIndicator doubleValue]);

    [request setDelegate:self];
    [request startAsynchronous];
}


- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
//  NSString *responseString = [request responseString];

    // Use when fetching binary data
//  NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
//  NSError *error = [request error];
}

-(NSString*)pathForLogInCCID{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *DataPath = [documentsDirectory stringByAppendingPathComponent:@"mySong.png"];
    return DataPath;
}
sugar
+3  A: 

Just use the expectedContentSize on the NSURLResponse object which is passed to the following delegate methods:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

}
-(NSURLRequest *)connection:(NSURLConnection *)connection
        willSendRequest:(NSURLRequest *)request
       redirectResponse:(NSURLResponse *)response {
}

like this:

long long size = [response expectedContentLength];
FenchKiss Dev
that was really a good solution, thanks
RVN