- (void)downloadWithUrl:(NSString*)targetURL
{
NSString *urlAsString = targetURL;
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *responce, NSData *data, NSError *error) {
if ([data length] > 0 && error == nil) {
//取得文件目錄
NSString* documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//完整檔案路徑
NSString* filePath = [documentsDir stringByAppendingString:@"ht-inception-360p.mp4"];
//將資料寫入檔案
[data writeToFile:filePath atomically:YES];
NSLog(@"成功存檔至%@",filePath);
}
else if ([data length] == 0 && error == nil){
NSLog(@"沒有被下載");
}
else if (error != nil){
NSLog(@"Err : %@",error);
}
}];
}
//判斷檔案是否存在
if (![[NSFileManager defaultManager] fileExistsAtPath:@"/你的本地端位置/檔名.mp4"]){
NSLog(@"檔名.mp4不存在");
}else{
NSLog(@"檔名.mp4存在");
}
沒有留言:
張貼留言