根据url地址下载文件到本地,返回本地文件地址
-(NSString*)DownloadTextFile:(NSString*)fileUrl fileName:(NSString*)_fileName
{
NSString *sandboxPath = NSHomeDirectory();
NSString *documentPath = [sandboxPath stringByAppendingPathComponent:@"Documents"];//将Documents添加到sandbox路径上
NSString *FileName=[documentPath stringByAppendingPathComponent:_fileName];//fileName就是保存文件的文件名
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:FileName])
{
return FileName;
}else
{
NSURL *url = [NSURL URLWithString:fileUrl];
NSLog(fileUrl);
NSData *data = [NSData dataWithContentsOfURL:url];
[data writeToFile:FileName atomically:YES];//将NSData类型对象data写入文件,文件名为FileName
}
return FileName;
}
/打开本地文件,获取文件路径
NSString *FileName = @"myWebURL.plist";
NSArray *cachePath= NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDir = [cachePath objectAtIndex:0];
NSString *FilePath = [cacheDir stringByAppendingPathComponent:FileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
// Copy the file from the resourcepath to the documentpath
if (![fileManager fileExistsAtPath:FilePath])
{
NSString *filePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:FileName];
NSError *error;
[fileManager copyItemAtPath:filePathFromApp toPath:FilePath error:&error];
}
NSLog(FilePath);
还没有评论,来说两句吧...