標籤:
擷取沙箱中的Documents地址的代碼。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];
假設視頻檔案的網路url為http://www.abc.com/test.mp4,下載到iOS沙箱中的檔案名稱為test.mp4。
利用如下代碼便可實現視頻檔案的下載和儲存, 注意在產生網路url的時候使用NSURL的類方法URLWithString,這樣才可以請求網路url,使用fileURLWithPath用來訪問本地檔案。這是我遇到的一個不大不小的坑,浪費了不少時間。
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];NSString *fullPath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"test.mp4"];NSURL *url = [NSURL URLWithString:@"http://www.abc.com/test.mp4"];NSURLRequest *request = [NSURLRequest requestWithURL:url];NSURLSessionDownloadTask *task =[manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { return [NSURL fileURLWithPath:fullPath]; } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { }];[task resume];
簡進祥===AFNetWorking 下載視頻檔案