如何在iphone sdk中监控pdf下载过程 [英] How to monitor pdf download process in iphone sdk

查看:103
本文介绍了如何在iphone sdk中监控pdf下载过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我需要从url.i下载pdf文件,知道如何从url下载pdf文件并存储在本地文档目录中。但是我需要显示下载过程,我想知道下载是否完成。请任意身体给出一个想法..

In my application i need to download pdf file from url.i know how to download pdf file from url and store in local document directory.But i need to show downloading process and i want to know whether download is completed.Please any body give an idea..

这里我的代码:

 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];


推荐答案

使用ASIHTTPRequest下载文件。对于以下代码,我使用了ASIHTTPRequest

Use ASIHTTPRequest for download file. for below code I had used 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 *strAudioURL=@"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

You can used the NSURLSession method to implement such scenario NSURLSession

这篇关于如何在iphone sdk中监控pdf下载过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆