携手创造,一起成长!这是我参与「日新计划 8 月更文挑战」的第28天,点击查看活动概况
前言
- 运用dataWithContentsOfURL进行视频下载,并保存到相册
- 从沙盒获取信息:NSFileManager类首要对文件的操作(删去、修改、移动、仿制等等) NSFileHandle 类首要对文件内容进行读取和写入操作
I NSFileManager的运用
NSFileHandle对文件内容进行读取和写入例子:运用dataWithContentsOfURL进行视频下载,并保存到相册
NSFileManager类对文件的操作
1.1 视频下载,并保存到相册
- 运用dataWithContentsOfURL进行视频下载,并保存到相册
NSFileManager *fileManage = [NSFileManager defaultManager];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:videoUrl]];
if (data == nil)
{
NSLog(@"网络出错,请稍后再试");
}
else
{
//用单例类 NSFileManager的方针,将文件写入本地
BOOL isSuccess = [fileManage createFileAtPath:path contents:data attributes:nil];
if (isSuccess)
{
NSLog(@"视频下载成功");
// 保存视频到相册
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:path]
completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"Save video fail:%@",error);
} else {
//2018-09-13 20:03:33.870 WeChat[6484:1077151] [MMVideoCompressHelper getCacheFilePathFrom:file:///var/mobile/Media/DCIM/100APPLE/IMG_0041.mp4 ]
NSLog(@"Save video succeed.:%@",assetURL);//assets-library://asset/asset.mp4?id=45C3D675-C625-4C52-B133-66D0A709AC57&ext=mp4
// 获取相册的最新一条视频的path,进行SightDraft的创立
}
}];
}
else
{
NSLog(@"视频下载失利");
}
1.2 NSFileManager类对文件的操作:删去、修改、移动、仿制
- [在sb tweak 中 复制特定文件到特定app的沙盒途径]
<script src="https://gist.github.com/zhangkn/74ce4737a33221228244b9b18f8e545a.js"></script>
%new
- (void)setupkeyword{// /var/mobile/Media/keyword.txt -> sanboxpath/Documents/keyword.txt
SBApplicationController *sbApplicationCtrl=[%c(SBApplicationController) sharedInstance];
id app = [sbApplicationCtrl applicationWithBundleIdentifier:@"com.tencent.xin"];
NSString *contentUserIDURL = [app sandboxPath];
//Library
//Documents
//tmp keyword.txt
NSString *realFile = [[contentUserIDURL stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"keyword.txt"];
//复制文件到这个地方,即可
//从某个途径读取文件内容 最好放在 /var/mobile/Media/keyword.txt -> realFile
NSLog(@"knrealFile %@",realFile);
NSString *tmpfile = @"/var/mobile/Media/keyword.txt";
NSString *content = [NSString stringWithContentsOfFile:tmpfile encoding:NSUTF8StringEncoding error:nil];
NSData *data = [content dataUsingEncoding: NSUTF8StringEncoding];
//写到方针文件
[data writeToFile:realFile atomically:YES];//掩盖
}
- 使用enumeratorAtPath,遍历NSDirectoryEnumerator 字典
- 使用contentsOfDirectoryAtPath,遍历files数组
#import "KNdelateDirTool.h"
@implementation KNdelateDirTool
//使用enumeratorAtPath,便当NSDirectoryEnumerator 字典
static id setupdeletedir(NSString* path) {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError* err = nil;
NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtPath:path];//获取NSDictionary
NSString *file;
while ((file = [dirEnum nextObject]))//遍历NSDictionary
// if ([file hasSuffix:@"/Documents"] || [file hasSuffix:@"/Library"] || [file hasSuffix:@"/tmp"] || [file hasSuffix:@"/StoreKit"]) 过滤条件
{
[fileManager removeItemAtPath:[path stringByAppendingPathComponent:file] error:&err];
// [fileManager createDirectoryAtPath:[path stringByAppendingPathComponent:file] withIntermediateDirectories:NO attributes:[NSDictionary dictionaryWithObjectsAndKeys:@"mobile", NSFileOwnerAccountName, @"mobile", NSFileGroupOwnerAccountName, nil] error:nil];
if (err) {
NSLog(@"setupdeletedir err:%@", err);
return @[@(NO), err];// 失利和错误信息
}
}
return @[@(YES)];// 成功
}
//使用contentsOfDirectoryAtPath,遍历files数组
static id kncleanDir(NSString* path, NSString* reg) {
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *files = [fm contentsOfDirectoryAtPath:path error:NULL];
for (NSString* file in files) {
if (knmathFile(file, reg)) {// 正则表达式进行过滤,使用NSPredicate 实现,更多信息请看这儿:谓词NSPredicate技术的使用。https://blog.csdn.net/z929118967/article/details/74066170
NSString* fullpath = [path stringByAppendingPathComponent:file];//途径的拼接
NSError* err = nil;
[fm removeItemAtPath:fullpath error:&err];//移除
if (err) {
NSLog(@"kncleanDir err:%@", err);
return @[@(NO), err];
}
}
}
return @[@(YES)];
}
//谓词技术的运用 evaluateWithObject
static BOOL knmathFile(NSString* file, NSString* reg) {
if (!reg)
return YES;
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",reg];
return [predicate evaluateWithObject:file];
}
@end
- 其他代码片段
github.com/kunnan/KNio…
gist.github.com/zhangkn/cd3…
code: KNiosreTool存储一些逆向剖析的东西代码片段: 清理文件、主动登录Wi-Fi
github.com/kunnan/KNio…
获取资源可重视公号:iOS逆向
II iOS保存图片到相册
blog.csdn.net/z929118967/…