溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

UI中的網(wǎng)絡(luò)請求

發(fā)布時間:2020-08-06 06:30:32 來源:網(wǎng)絡(luò) 閱讀:224 作者:ladispartion1 欄目:開發(fā)技術(shù)

@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問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI