Создайте одно приложение с одной страницей. На этой странице есть одна кнопка, метка & - progressview. Я хочу, чтобы начать загрузку при нажатии на кнопку и показать прогресс & статус метки скачать.Создайте динамический UILabel, показывая статус загруженного файла с 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];
}
Пожалуйста, расскажите, как показать статус в UILabel
с NSURLConnection
.
мой друг, я получил одну ошибку в последней строке: «Слишком много аргументов для вызова метода» – emma
был на самом деле не на моем Mac поэтому не может проверить. В любом случае, похоже, кто-то копировал мой ответ: p вместо его редактирования. все еще Отредактировано ans now – Bonnie