iOS制作文件管理小程序
2020-12-01 本文已影响0人
我不白先生
首先就viewcotroller.h和.m以及故事板删除

AppDelegate.m
@implementation AppDelegate
@synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window =[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[FilesTableViewController new]];
[self.window makeKeyAndVisible];
return YES;
}
新建一个tableViewController
FilesTableViewController.h
@interface FilesTableViewController : UITableViewController
@property(nonatomic,copy)NSString *path;
@end
FilesTableViewController.m
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
#import "FilesTableViewController.h"
@interface FilesTableViewController ()
@property(nonatomic,strong)NSMutableArray *filePaths;
@property(nonatomic,strong)AVAudioPlayer *player;
@end
@implementation FilesTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = self.path? self.path : @"/Users/zhengyang";
self.title = [path lastPathComponent];
self.filePaths =[NSMutableArray array];
NSArray *fileNames = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:path error:nil];
for (NSString *fileName in fileNames) {
if ([fileName hasPrefix:@"."]) {
continue;
}
NSString *filePath = [path stringByAppendingPathComponent:fileName];
[self.filePaths addObject:filePath];
}
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.filePaths.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(!cell){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
NSString *filePath = self.filePaths[indexPath.row];
cell.textLabel.text = [filePath lastPathComponent];
//判断是否是文件夹
if ([self isDirectoryOfpath:filePath]) {
cell.imageView.image = [UIImage imageNamed:@"QQ20201201-2.png"];
}else{
cell.imageView.image = [UIImage imageNamed:@"QQ20201201-0.png"];
}
return cell;
}
-(BOOL)isDirectoryOfpath:(NSString*)path{
NSFileManager *fm = [NSFileManager defaultManager];
BOOL isDir = NO;
//方法返回值判断的是文件是否存在
if([fm fileExistsAtPath:path isDirectory:&isDir]&&isDir){
return YES;
}
return NO;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *filePath = self.filePaths[indexPath.row];
//判断是否是文件夹
if([self isDirectoryOfpath:filePath]){
FilesTableViewController *vc = [FilesTableViewController new];
vc.path = filePath;
[self.navigationController pushViewController:vc animated:YES];
}else if([filePath hasSuffix:@"mp4"]){
AVPlayerViewController *vc = [AVPlayerViewController new];
vc.player = [[AVPlayer alloc]initWithURL:[NSURL fileURLWithPath:filePath]];
[vc.player play];
[self.navigationController pushViewController:vc animated:YES];
}else if([filePath hasSuffix:@"jpg"]||[filePath hasSuffix:@"png"]){
UIViewController *vc = [UIViewController new];
vc.view.backgroundColor = [UIColor whiteColor];
UIImageView *iv = [[UIImageView alloc]initWithFrame:self.view.bounds];
[vc.view addSubview:iv];
iv.image = [UIImage imageWithContentsOfFile:filePath];
[self.navigationController pushViewController:vc animated:YES];
}else if([filePath hasSuffix:@"mp3"]){
self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:filePath] error:nil];
[self.player play];
}else if([filePath hasSuffix:@"rxf"]||[filePath hasSuffix:@"txt"]||[filePath hasSuffix:@".h"]||[filePath hasSuffix:@".m"])
{
UIViewController *vc = [UIViewController new];
vc.view.backgroundColor = [UIColor whiteColor];
UITextView *tv = [[UITextView alloc]initWithFrame:self.view.bounds];
[vc.view addSubview:tv];
tv.text = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[self.navigationController pushViewController:vc animated:YES];
}else{
UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"提示" message:@"目前版本暂不支持此格式,请期待下一版本!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
[ac addAction:action1];
[self presentViewController:ac animated:YES completion:nil];
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSString *filePath = self.filePaths[indexPath.row];
[[NSFileManager defaultManager]removeItemAtPath:filePath error:nil];
[self.filePaths removeObject:filePath];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
}
}
屏幕效果图可以通过APP直接播放mp3、mp4、图片、文本、删除文件(删除文件时要小心,删除了回收站也不能找回来)等
