国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

斷點下載

2019-11-14 18:03:41
字體:
來源:轉載
供稿:網友
 1 @interface ViewController ()<NSURLConnectionDataDelegate> 2 { 3     // 文件的總大小 4     long long _total; 5     // 當前下載 6     long long _current; 7 } 8  9 // 文件句柄10 @PRoperty (nonatomic,strong)NSFileHandle *fileHandle;11 // 連接對象12 @property (nonatomic,strong)NSURLConnection *conn;13 @property (weak, nonatomic) IBOutlet UiprogressView *progress;14 @property (weak, nonatomic) IBOutlet UIButton *button;15 @end16 17 @implementation ViewController18 - (void)viewDidLoad {19     [super viewDidLoad];20     self.progress.progress = 0;21     // Do any additional setup after loading the view, typically from a nib.22 }23 24 - (IBAction)buttonAction:(UIButton *)sender {25     if ([[sender titleForState:UIControlStateNormal]isEqualToString:@"開始"]) {26         [sender setTitle:@"暫停" forState:(UIControlStateNormal)];27         // 請求28         NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:kUrl]];29         // 設置請求參數30         NSString *rang = [NSString stringWithFormat:@"byte:%lld",_current];31         NSLog(@"request:%@end",request);32         [request setValue:rang forHTTPHeaderField:@"Range"];33          NSLog(@"request:%@end",request);34         // 建立連接35         self.conn = [NSURLConnection connectionWithRequest:request delegate:self];36         // 開始請求37         [_conn start];38     }else{39         [sender setTitle:@"開始" forState:(UIControlStateNormal)];40         // 取消41         [_conn cancel];42     }43 }44 45 - (void)didReceiveMemoryWarning {46     [super didReceiveMemoryWarning];47     // Dispose of any resources that can be recreated.48 }49 50 #pragma mark---delegate51 // 收到響應52 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response53 {54     // 獲取總文件大小(總文件的大小在響應里面可以獲得)55     _total = response.expectedContentLength;56     // 創建文件57     NSFileManager *manager = [NSFileManager defaultManager];58     NSString *cachesPath  = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];59     NSString *filePath = [cachesPath stringByAppendingPathComponent:@"music.mp3"];60     NSLog(@"%@",cachesPath);61     // 創建文件62     [manager createFileAtPath:filePath contents:nil attributes:nil];63     // 讓文件句柄指向文件64     self.fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];65 }66 // 接收到數據67 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data68 {69     // 計算當前總大小70     _current += data.length;71     // 更新 (在子線程中不能更新UI)72     [self performSelectorOnMainThread:@selector(updateProgress) withObject:self waitUntilDone:YES];73     // 指向文件末尾74     [self.fileHandle seekToEndOfFile];75     // 寫數據76     [self.fileHandle writeData:data];77 }78 // 結束79 - (void)connectionDidFinishLoading:(NSURLConnection *)connection80 {81     NSLog(@"下載完畢");82 }83 // 更新progress84 - (void)updateProgress85 {86     self.progress.progress = (long double)_current / (long double)_total;87 }88 89 @end

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 仙游县| 瑞金市| 高阳县| 铜陵市| 隆化县| 乃东县| 拉萨市| 巢湖市| 商城县| 湛江市| 常熟市| 万州区| 临朐县| 东乡| 福鼎市| 思茅市| 元氏县| 安溪县| 阳信县| 辰溪县| 色达县| 尖扎县| 嫩江县| 双辽市| 盐城市| 扬中市| 泸州市| 南京市| 噶尔县| 义乌市| 南木林县| 武穴市| 南木林县| 连城县| 凤山县| 平安县| 台中县| 客服| 吴桥县| 乌苏市| 唐海县|