-(void)downloadWithUrl:(NSURL *)url compelet:(DownLoadBlock)block {
//保存block
self.block = block;
//創建隊列
dispatch_queue_t queue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
//執行下載任務
//創建請求
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
//獲取數據
NSData *date = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//將數據返回給調用者
dispatch_sync(dispatch_get_main_queue(), ^{
self.block(date);
});
});
}
獲取數據的方法好像是ios9.0之後就不能用了,現在怎麼獲取,
NSURLConnection已經被iOS過期了,需要用新的API NSURLSession,它只有異步的方式