温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

UI中的网络请求

发布时间:2020-08-06 06:30:32 来源:网络 阅读:231 作者:ladispartion1 栏目:开发技术

@interface ViewController ()<NSURLConnectionDataDelegate>

{

    CGFloat totleLength;

    NSMutableData *filedata;

    BOOL isDownload;

    

    CGFloat reciveTotle;

    NSString *filePath;

    NSURLConnection *_connection;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    totleLength = [[userDefaults objectForKey:@"totleLength"] floatValue];

    reciveTotle = [[userDefaults objectForKey:@"reciveTotle"] floatValue];

    if (reciveTotle > 0) {

        CGFloat progress = reciveTotle / totleLength;

        self.progressView.progress = progress;

        self.progressLabel.text = [NSString stringWithFormat:@"%.f%%",progress * 100];

    }

}

- (IBAction)btnClick:(UIButton *)sender {

    if (isDownload) {

        return;

    }

    NSURL *url = [NSURL URLWithString:@"http://free2.macx.cn:8182/game/BombSquadX401.dmg"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    

    if (reciveTotle > 0) {

        NSString *value = [NSString stringWithFormat:@"bytes=%d-",(int)reciveTotle];

        [request setValue:value forHTTPHeaderField:@"Range"];

    }

    

    

   _connection = [NSURLConnection connectionWithRequest:request delegate:self];

    isDownload = true;

    

    NSString *str = url.absoluteString;

    NSString *strName = [str lastPathComponent];

    filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@",strName];

    

    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {

        

        [[NSFileManager defaultManager]createFileAtPath:filePath contents:nil attributes:nil];

    }


}

- (IBAction)pauseAction:(UIButton *)sender {

    

    [_connection cancel];

    _connection  = nil;

    

    [self appendFileData:filedata];

    

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    [userDefaults setObject:@(reciveTotle) forKey:@"reciveTotle"];

    [userDefaults setObject:@(totleLength) forKey:@"totleLength"];

    

    [userDefaults synchronize];

    

    isDownload = NO;

    

}

#pragma mark-NSURLConnectionDataDelegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response{

    

    filedata = [[NSMutableData alloc]init];

    NSDictionary *dic = response.allHeaderFields;

    NSNumber *number = [dic objectForKey:@"Content-Length"];

    totleLength = [number floatValue];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    

    [filedata appendData:data];

    reciveTotle += data.length;

    self.progressView.progress = reciveTotle / totleLength;

    self.progressLabel.text = [NSString stringWithFormat:@"%.f%%",self.progressView.progress * 100];

    if (filedata.length >= 500 * 1000) {

        [self appendFileData:filedata];

        filedata.data = nil;

    }

}


- (void)appendFileData:(NSData *)data

{

    if (data.length == 0) {

        return;

    }

    NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];

    [fileHandle seekToEndOfFile];

    [fileHandle writeData:data];

    

    [fileHandle closeFile];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    

    if(filedata.length < 500 * 1000){

        [self appendFileData:filedata];

        [filedata setData:nil];

        

         [filedata writeToFile:filePath atomically:YES];

    }

    self.progressLabel.text = @"下载完成";

    isDownload = false;

    

}



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI