【问题标题】:Objective C: Downloading File With Progress Bar [duplicate]目标 C:下载带有进度条的文件 [重复]
【发布时间】:2013-05-09 03:05:06
【问题描述】:

我正在尝试放置一个在下载过程中同步的进度条。 我的应用现在可以使用此代码下载文件...

    pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://webaddress.com/pro/download/file.pdf"]];

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

    pdfFilePath = [resourcePDFPath stringByAppendingPathComponent:@"myPDF.pdf"];

    [pdfData writeToFile:pdfFilePath atomically:YES];

在这段代码的过程中,应用程序在下载过程中停止了,这是否正常? 现在我想要的是在下载的停止时间放置一个进度条。

我尝试查看我在网上找到的代码,但我有点困惑,我想我需要一个逐步解释清楚的参考。

【问题讨论】:

    标签: ios objective-c download uiprogressview


    【解决方案1】:

    使用AFNetworking

    这里progress是UIProgressview

    #import <AFNetworking/AFNetworking.h>//add to the header of class
    
    -(void)downloadShowingProgress
    {
       progress.progress = 0.0;
    
        currentURL=@"http://www.selab.isti.cnr.it/ws-mate/example.pdf";
    
    
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]];
        AFURLConnectionOperation *operation =   [[AFHTTPRequestOperation alloc] initWithRequest:request];
    
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"MY_FILENAME_WITH_EXTENTION.pdf"];
        operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
    
        [operation setDownloadProgressBlock:^(NSUInteger bytesRead, NSUInteger totalBytesRead, NSUInteger totalBytesExpectedToRead) {
            progress.progress = (float)totalBytesRead / totalBytesExpectedToRead;
    
        }];
    
        [operation setCompletionBlock:^{
            NSLog(@"downloadComplete!");
    
        }];
        [operation start];
    
    }
    

    使用 NSURLConnection

    -(void)downloadWithNsurlconnection
    {
    
        NSURL *url = [NSURL URLWithString:currentURL];
        NSURLRequest *theRequest = [NSURLRequest requestWithURL:url         cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
        receivedData = [[NSMutableData alloc] initWithLength:0];
        NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self     startImmediately:YES];
    
    
    }
    
    
    - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        progress.hidden = NO;
        [receivedData setLength:0];
        expectedBytes = [response expectedContentLength];
    }
    
    - (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        [receivedData appendData:data];
        float progressive = (float)[receivedData length] / (float)expectedBytes;
        [progress setProgress:progressive];
    
    
    }
    
    - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
    }
    
    - (NSCachedURLResponse *) connection:(NSURLConnection *)connection willCacheResponse:    (NSCachedURLResponse *)cachedResponse {
        return nil;
    }
    
    - (void) connectionDidFinishLoading:(NSURLConnection *)connection {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:[currentURL stringByAppendingString:@".mp3"]];
        NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [receivedData writeToFile:pdfPath atomically:YES];
        progress.hidden = YES;
    }
    

    【讨论】:

    • 它说 AFURLConnectionOperation 是一个未声明的标识符
    • 编辑了答案。必须在类头中包含#import
    • 我试过了,但没有找到。我应该使用什么框架?
    • 在标题 AFNetworking 中添加了框架的链接
    • 什么是预期字节??很抱歉问了这么多问题。
    【解决方案2】:

    使用 ASIHTTPRequest.h 类和 ASINetworkQueue.h 下载文件。

    并将此代码用于进度条

        request = [ASIHTTPRequest requestWithURL:@"http://webaddress.com/pro/download/file.pdf];
        [request setDelegate:self];
        [request setDownloadProgressDelegate:progressView];
        [request setShowAccurateProgress:YES];
        request.shouldContinueWhenAppEntersBackground=YES;
        request.allowResumeForFileDownloads=YES;
        [request startAsynchronous];
    

    这可能对你有帮助

    【讨论】:

    • 我应该把这个放在点击“下载”按钮上吗?
    • 是的,您想将此代码放在按钮单击功能中...
    【解决方案3】:

    恐怕不正常,使用异步方法获取NSData。

    【讨论】:

      【解决方案4】:

      首先你应该清楚是同步调用还是异步调用。 对于移动应用或任何其他应用,首选异步方式。

      一旦你清楚使用 NSURLConnection 类从 URL 中获取数据。 这是good tutorial

      对于加载,您可以在启动请求时启动进度,并在收到 connection:didFailWithError:connectionDidFinishLoading: 委托方法时停止它。

      【讨论】:

      • 如何在 swift @Inder Kumar Rathore 中使用 NSURLConnection 同步下载 pdf 文件
      猜你喜欢
      • 1970-01-01
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多