在下载iPhone的许多文件时添加activityIndi​​cator

时间:2012-12-01 11:37:09

标签: iphone ios ipad uiactivityindicatorview

我正在使用此代码下载许多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的新手,我不知道该怎么做。 有谁可以帮助我?

5 个答案:

答案 0 :(得分:2)

活动指标有很多自定义类。因为你是iphone的新手。我建议使用xcode中的默认活动指示。

1)将UIActivityIndi​​cator对象拖放到Interface构建器。

将以下代码写入viewcontroller的.h文件中:

    IBOutlet  UIActivityIndicator *activityIndicator;
    //connect this outlet to the xib.
你的.m文件中的

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

希望这有助于您开始。

答案 1 :(得分:1)

我找到了一个很好的例子

<。>文件中的

UIAlertView *progressAlert;
<。>文件中的

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

答案 2 :(得分:0)

MBProgressuHUD是一个非常好且易于使用的插入式课程,用于显示活动指示器。

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

答案 3 :(得分: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.

答案 4 :(得分:0)

  1. 将其卸载到第二个帖子:

    [self performSelectorInBackground:@selector(downloadAll)];
    
  2. 在下载前显示指标用户界面

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

    - (void)downloadAll {
        //...... YOUR DOWNLOAD CODE
        [self performSelectorOnMainThread:@selector(downloadDone) waitUntilDone:NO];
    }
    
  4. 在downloadDone hide indiator

    - (void)downloadDone {
        [self.indicator stopAnimating];
        [self.indicator removeFromSuperview];
    }