IOS文件下载

时间:2023-03-09 17:25:38
IOS文件下载

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask,YES);//使用C函数NSSearchPathForDirectoriesInDomains来获得沙盒中目录的全路径。

//  ***  NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *ourDocumentPath =[documentPaths objectAtIndex:0];

NSString *sandboxPath = NSHomeDirectory();

//    NSString *documentPath = [sandboxPath  stringByAppendingPathComponent:@"Purchase.app"];//将Documents添加到sandbox路径上//stringByAppendingPathComponent

NSString *FileName=[ourDocumentPath stringByAppendingPathComponent:_fileName];//fileName就是保存文件的文件名

NSLog(FileName);

NSFileManager *fileManager = [NSFileManager defaultManager];

// Copy the database sql file from the resourcepath to the documentpath

if ([fileManager fileExistsAtPath:FileName])

{

return FileName;

}else

{

NSURL *url = [NSURL URLWithString:fileUrl];

NSData *data = [NSData dataWithContentsOfURL:url];

[data writeToFile:FileName atomically:YES];//将NSData类型对象data写入文件,文件名为FileName

}

return FileName;