2012-08-24 29 views
0

在我的應用程序中,我需要從url.i下載pdf文件知道如何從網址下載pdf文件並存儲在本地文檔目錄中。但是我需要顯示下載過程,並且我想要要知道下載是否completed.Please任何機構給出一個想法..如何監控iphone中的pdf下載過程sdk

這裏我的代碼:

NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.msy.com.au/Parts/PARTS.pdf"]]; 

//Store the Data locally as PDF File 

NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]]; 

NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"myPDF.pdf"]; 

[pdfData writeToFile:filePath atomically:YES]; 

回答

3

使用ASIHTTPRequest下載文件。下面的代碼我用了ASIHTTPRequest

float currentProgress; 
UILabel *dwnLbl; 
UIProgressView * myProgressIndicator; 
UIProgressView *progressBar; 
@property (nonatomic, retain) ASIHTTPRequest *rqstForAudio; 


-(void)viewDidLoad{ 
    self.av=[[UIAlertView alloc] initWithTitle:@"Downloading.." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; 
    [self.actV setFrame:CGRectMake(125, 60, 37, 37)]; 

    dwnLbl = [[UILabel alloc] initWithFrame:CGRectMake(45, 30, 200, 37)]; 
    dwnLbl.textAlignment = UITextAlignmentCenter; 
    dwnLbl.font = [UIFont boldSystemFontOfSize:20]; 
    dwnLbl.backgroundColor = [UIColor clearColor]; 
    dwnLbl.textColor = [UIColor whiteColor]; 

    progressBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar]; 
    [progressBar setFrame:CGRectMake(45, 65, 200, 20)]; 
    progressBar.progress = 0; 
    [self.av addSubview:dwnLbl]; 
    [self.av addSubview:progressBar]; 
} 
-(void)downLoadBook{ 
    NSString *[email protected]"http://www.msy.com.au/Parts/PARTS.pdf" 
    // check first locally exists or not 
    NSString *strPathToAudioCache=[NSString stringWithFormat:@"%@/%@", 
            [(NSArray*)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0], 
            AudioFolder]; 

    NSDictionary *dOfAudios=[NSDictionary dictionaryWithContentsOfFile:strPathToAudioCache]; 
    if([dOfAudios valueForKey:strAudioURL]) { 
    } else { 
     self.av.title = @"Downloading.."; 
     [self.av show]; 
     NSString *pdf = @"bookTitle.pdf"; 


     NSURL *audioURL = [NSURL URLWithString:strAudioURL]; 
     NSString *strPathToDownload=[NSString stringWithFormat:@"%@/%@",[(NSArray*)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0],pdf]; 
     [self.rqstForAudio setDownloadProgressDelegate:myProgressIndicator]; 

     if(!self.rqstForAudio || [self.rqstForAudio isFinished]) { 
      self.rqstForAudio=[ASIHTTPRequest requestWithURL:audioURL]; 
      [self.rqstForAudio setDelegate:self]; 
      [self.rqstForAudio setDownloadProgressDelegate:self]; 
      [self.rqstForAudio setAllowResumeForFileDownloads:YES]; 
      [self.rqstForAudio setCachePolicy:ASIUseDefaultCachePolicy]; 
      [self.rqstForAudio setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; 
      [self.rqstForAudio setDidFailSelector:@selector(failedToLoad:)]; 
      [self.rqstForAudio setDidFinishSelector:@selector(finishedLoading:)]; 
      [self.rqstForAudio setDownloadCache:[ASIDownloadCache sharedCache]]; 
      [self.rqstForAudio setDownloadDestinationPath:strPathToDownload]; 
      [self.rqstForAudio startAsynchronous]; 
     } 
    } 
} 



- (void)failedToLoad:(ASIHTTPRequest*)request { 
    [self.av dismissWithClickedButtonIndex:0 animated:YES]; 
    NSLog(@"failed to download"); 
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"MESSAGE" message:@"Failed to Download" delegate:self cancelButtonTitle:RETRY otherButtonTitles:nil, nil]; 
    av.delegate = self; 
    [av show]; 
} 

- (void)finishedLoading:(ASIHTTPRequest*)request { 
    NSLog(@"finished loading"); 
    NSString *strPathToAudioCache=[NSString stringWithFormat:@"%@", 
           [(NSArray*)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]]; 

NSMutableDictionary *dOfAudios=[NSMutableDictionary dictionaryWithContentsOfFile:strPathToAudioCache]; 

if([dOfAudios allKeys].count>0) { 
    [dOfAudios setValue:[request downloadDestinationPath] forKey:[[request url] description]]; 
} else { 
    dOfAudios=[NSMutableDictionary dictionary]; 
    [dOfAudios setValue:[request downloadDestinationPath] forKey:[[request url] description]]; 
} 
    [self.av dismissWithClickedButtonIndex:0 animated:YES]; 
    [dOfAudios writeToFile:strPathToAudioCache atomically:YES]; 
} 

- (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes{ 
    [self setProgress:[myProgressIndicator progress]]; 
} 
- (void)setProgress:(float)progress 
{ 
    currentProgress = progress; 
    if (!progress == 0.0) { 
    } 
    if(currentProgress*100 == 100.00){ 
     self.av.title = @"Finishing.."; 
    } 
    progressBar.progress = currentProgress; 
    dwnLbl.text = [NSString stringWithFormat:@"%.2f%%",currentProgress*100]; 
} 

編輯

您可以使用NSURLSession方法來實現這樣的場景 NSURLSession

+0

由於其工作 – banu

+0

myProgressIndicator顯示錯誤「使用未聲明的標識符‘myProgressIndicator’的」 – banu

+0

只是在頭文件中定義爲UIProgressBar * myProgressIndicator – Hiren

1

我會強烈建議在ASIHTTPRequest,便於文件下載考慮看看。

通過您可以使用下載進度的功能數量。

+0

感謝薩穆埃爾我將把ASIHTTPRequest。 – banu