【问题标题】:How to monitor pdf download process in iphone sdk如何在 iphone sdk 中监控 pdf 下载过程
【发布时间】:2012-08-19 15:47:59
【问题描述】:

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

这里是我的代码:

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

【问题讨论】:

    标签: iphone ios ios4 iphone-sdk-3.0


    【解决方案1】:

    使用 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 *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

    【讨论】:

    • myProgressIndicator 显示错误“使用未声明的标识符'myProgressIndicator'”
    • 刚刚在头文件中定义为 UIProgressBar *myProgressIndicator
    • 还有“AudioFolder”未声明的标识符,你能解释一下吗
    • 好的,你必须在你的实现文件中#define AudioFolder @"AudioFolder.plist"
    • 非常感谢,现在我看到了我的下载进度!伟大的可可很重要..:)
    【解决方案2】:

    我强烈建议您查看ASIHTTPRequest,以便轻松下载文件。

    您可以使用下载进度的功能编号。

    【讨论】:

    • 谢谢 samuel,我会参考 ASIHTTPRequest。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    相关资源
    最近更新 更多