使用NSURLConnection创建显示状态下载文件的动态UILabel

时间:2013-05-09 08:11:01

标签: ios objective-c uilabel download nsurlconnection

我创建了一个包含一个页面的应用程序。在该页面中有一个按钮,标签&进度观点。我想要的是点击按钮开始下载并显示进度&标签状态下载。

我可以制作进度视图,但我无法制作显示下载状态的标签。

例如,我希望我的标签显示"12 MB downloaded of 100 MB"

这是我的代码:

查看controller.h

@interface ViewController : UIViewController
{
    float expectedBytes;
}
@property (weak,nonatomic) IBOutlet UIProgressView *progressView;
@property (strong,nonatomic) IBOutlet UIButton *download;
- (IBAction)download:(id)sender;

@end

查看controller.m

@implementation ViewController
{
    NSMutableData *receivedData;
    NSString *currentURL;
    NSString *name;
}
- (void)viewDidLoad
{
    currentURL = @"http://192.168.1.100/mamal/Adele%20-%20Someone%20Like%20You%20(MTV%20Video%20Music%20Awards%202011)%20HD%20Live.mkv";
    name = [currentURL lastPathComponent];
    [super viewDidLoad];
}
@synthesize progressView,download;
-(IBAction)download:(id)sender
{
    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* foofile = [documentsPath stringByAppendingPathComponent:name];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];
    if (!fileExists)
    {
        [self downloadWithNsurlconnection];
    }
    else
    {
        NSLog(@"FILE EXIST");
    }
}
-(void)downloadWithNsurlconnection
{
    NSURL *url = [NSURL URLWithString:currentURL];
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:600];
    receivedData = [[NSMutableData alloc] initWithLength:0];
    NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
    [connection start];

}


- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    progressView.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;
    [progressView setProgress:progressive];
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"%@",paths);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:name];
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [receivedData writeToFile:pdfPath atomically:YES];
}

请指导我如何使用UILabelNSURLConnection中显示状态下载。

2 个答案:

答案 0 :(得分:0)

为标签

创建一个插座
@property (weak,nonatomic) IBOutlet UILabel *label;

然后在

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data];
    float progressive = (float)[receivedData length] / (float)expectedBytes;
    [progressView setProgress:progressive];
    [lable setText:[NSString stringWithFormat:@"%u MB of %f MB",[receivedData length],expectedBytes]];
}

您可以将字节更改为MB

答案 1 :(得分:0)

将此语法放在.h文件

@property (weak,nonatomic) IBOutlet UILabel *label;

然后在.m文件中

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data];
    float progressive = (float)[receivedData length] / (float)expectedBytes;
    [progressView setProgress:progressive];
    [lable setText:[NSString stringWithFormat:@"%u MB of %f MB",[receivedData length],expectedBytes]];

}