2

我正在使用此代码下载许多 pdf 文件

for (int i=0; i<[myBooks count]; i++) {

Book_own *temp= (Book_own *)[myBooks objectAtIndex:i]; // myBooks is a mutable array of object Book_own

     // to download pdf
     NSString *documentName = [temp.bo_path stringByDeletingPathExtension];

     NSString *pdfLink = [NSString stringWithFormat:@"http://url.com/files/%@",temp.bo_path];

     NSString *linkWithoutSpaces = [pdfLink stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

     NSString *urlString = linkWithoutSpaces;

     NSURL *url = [NSURL URLWithString:urlString];

     NSData *data = [NSData dataWithContentsOfURL:url];

     NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

     NSString *documentDir = [documentPaths objectAtIndex:0];

     NSLog(@"in settings, Document Directory: %@",documentDir);

     NSString *pdfPath = [NSString stringWithFormat:@"%@/%@",documentDir,[NSString stringWithFormat:@"%@.pdf",documentName]];

      NSLog(@"pdfpath: %@",pdfPath);

      [data writeToFile:pdfPath atomically:YES];
      NSData *tmp = [NSData dataWithContentsOfURL:url];

      if (tmp != nil) {
         NSError *error = nil;
         [tmp writeToFile:pdfPath options:NSDataWritingAtomic error:&error];
         if (error != nil) {
            NSLog(@"Failed to save the file: %@", [error description]);
             } else {
               NSLog(@"downloaded");
             }
           } else {
               NSLog(@"fail to save pdf file");
              }
 }

这会为我下载文件,但它让我等待了很长时间,我想添加 activityIndi​​cator 或进度条来显示下载进度。但我是 iPhone 的新手,我不知道该怎么做。谁能帮我?

4

5 回答 5

2

活动指示器有很多自定义类。由于您是 iphone 新手。我建议在 xcode 中使用默认活动指示器。

1) 将 UIActivityIndi​​cator 对象拖放到您的界面构建器中。

在视图控制器的 .h 文件中编写以下代码:

    IBOutlet  UIActivityIndicator *activityIndicator;
    //connect this outlet to the xib.

在您的 .m 文件中:

       //when u call the method
      [activityIndicator startanimating];
      //when everything is complete
      [activityIndicator stopanimating];

希望这可以帮助您开始。

于 2012-12-01T11:54:32.370 回答
1

我找到了一个很好的例子

在 .h 文件中

UIAlertView *progressAlert;

在 .m 文件中

-(void)showAlertMethod

{
 progressAlert = [[UIAlertView alloc] initWithTitle:@"Uploading please wait...\n" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
CGRect alertFrame = progressAlert.frame;
UIActivityIndicatorView* activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityIndicator.frame = CGRectMake(135,alertFrame.size.height+55, alertFrame.size.width,30);
activityIndicator.hidden = NO;
activityIndicator.contentMode = UIViewContentModeCenter;
[activityIndicator startAnimating]; 
[progressAlert addSubview:activityIndicator];
[progressAlert show];

}
-(void)dismissAlertMethod
{
[progressAlert dismissWithClickedButtonIndex:0 animated:YES];
}

根据您的要求调用该方法。我以这种方式调用方法:-

[NSThread detachNewThreadSelector:@selector(showAlertMethod) toTarget:self withObject:nil];

[NSThread detachNewThreadSelector:@selector(dismissAlertMethod) toTarget:self withObject:nil];
于 2012-12-01T22:12:31.143 回答
0

MBProgressuHUD是一个非常好用且易于使用的插件类来显示活动指示器。

此外,您绝对应该异步下载文件。在演示项目中,我认为有几个使用 NSURLConnection 和 GCD 的不同示例。

于 2012-12-01T11:47:19.353 回答
0
You can use below function for depicting indicator, which is added on a view:(showing indicator at amid of screen)

-(void)showActivityIndicatorViewer
{
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    UIWindow *window = delegate.window;
    activityView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, window.bounds.size.width, window.bounds.size.height)];
    activityView.backgroundColor = [UIColor blackColor];
    activityView.alpha = 0.5;

    UIActivityIndicatorView *activityWheel = [[UIActivityIndicatorView alloc] initWithFrame: CGRectMake(window.bounds.size.width / 2 - 12, window.bounds.size.height / 2 - 12, 24, 24)];
    activityWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    activityWheel.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
                                      UIViewAutoresizingFlexibleRightMargin |
                                      UIViewAutoresizingFlexibleTopMargin |
                                      UIViewAutoresizingFlexibleBottomMargin);
    [activityView addSubview:activityWheel];
    [window addSubview: activityView];

    [[[activityView subviews] objectAtIndex:0] startAnimating];
}

-(void)hideActivityIndicatorViewer
{
    [[[activityView subviews] objectAtIndex:0] stopAnimating];
    [activityView removeFromSuperview];
    activityView = nil;
}

While, if you just want default(shown at top of status bar):

Also, you can third party like HUD, you just google it, you'll find how yo use it.
于 2012-12-01T11:52:18.283 回答
0
  1. 将其卸载到第二个线程:

    [self performSelectorInBackground:@selector(downloadAll)];
    
  2. 下载前显示指示器 UI

    //MOCKUP UI
    indicator = [[UIActivityIndicator alloc] initWithFrame:CGRectMake(10,10,50,50)];
    self.view addSubView:indicator];
    [indicator startAnimating];
    
    [self performSelectorInBackground:@selector(downloadAll)];
    
  3. AFTER 下载回调主线程

    - (void)downloadAll {
        //...... YOUR DOWNLOAD CODE
        [self performSelectorOnMainThread:@selector(downloadDone) waitUntilDone:NO];
    }
    
  4. 在下载完成隐藏指示器

    - (void)downloadDone {
        [self.indicator stopAnimating];
        [self.indicator removeFromSuperview];
    }
    
于 2012-12-01T11:54:46.970 回答