ios文件夹共享Files

本文详细介绍了iOS 11之后苹果开放的文件共享功能,并通过实例演示如何使用Swift编程语言实现不同文件目录下的文件下载及管理。重点介绍了NSDocumentationDirectory等四种文件目录的区别及其在iOS文件管理中的应用。

ios11 之后苹果开放了共享文件

这里写图片描述

这个是干神马用的了 ?简单的说这货是把APP下载的文件给抡出来的一个东西。。但是,我们打开手机上的 “文件” 这个APP 发现里面神马都没有,这货不是管理手机上下载的文件的吗???

说到下载文件之前,我们得说到一个东西,叫做iOS 的文件目录 ,我们经常会对文件目录进行操作 ,我们熟悉以下(我们经常使用的)目录

NSDocumentationDirectory
NSDocumentDirectory
NSDownloadsDirectory
NSCachesDirectory

为了观察我们分别建立四个不同的文件夹 。。。

-(void)createDocumentationDirectory{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    path1 = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"directoryOne"];

    if(![fileManager fileExistsAtPath:path1]){
        [[NSFileManager defaultManager] createDirectoryAtPath:path1 withIntermediateDirectories:YES attributes:nil error:nil];
    }

}
-(void)createDocumentDirectory{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    path2 = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"directoryTwo"];

    if(![fileManager fileExistsAtPath:path2]){
        [[NSFileManager defaultManager] createDirectoryAtPath:path2 withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

-(void)createDownloadsDirectory{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES);
    path3 = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"directoryThree"];

    if(![fileManager fileExistsAtPath:path3]){
        [[NSFileManager defaultManager] createDirectoryAtPath:path3 withIntermediateDirectories:YES attributes:nil error:nil];
    }

}

-(void)createCachesDirectory{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    path4 = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"directoryFour"];

    if(![fileManager fileExistsAtPath:path4]){
        [[NSFileManager defaultManager] createDirectoryAtPath:path4 withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

分别在四个文件夹中下载文件 。。。

#import "ViewController.h"

#import "AFNetworking.h"


#define URL1 @"https://hebiao.online/video/1.mp4"
#define URL2 @"https://hebiao.online/video/2.mp4"
#define URL3 @"https://hebiao.online/video/3.mp4"
#define URL4 @"https://hebiao.online/video/4.mp4"

@interface ViewController (){
    NSString *path1,*path2,*path3,*path4;
    NSFileManager *fileManager;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    fileManager = [NSFileManager defaultManager];

    [self createDocumentationDirectory];
    [self createDocumentDirectory];
    [self createDownloadsDirectory];
    [self createCachesDirectory];

    UIButton *button1 = [[UIButton alloc] init];
    button1.frame = CGRectMake(20, 100, 150, 40);
    button1.backgroundColor = [UIColor lightGrayColor];
    [button1 setTitle:@"1苍井空" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(action1) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];



    button1 = [[UIButton alloc] init];
    button1.frame = CGRectMake(200, 100, 150, 40);
    button1.backgroundColor = [UIColor lightGrayColor];
    [button1 setTitle:@"2饭岛爱" forState:UIControlStateNormal];
     [button1 addTarget:self action:@selector(action2) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];

    button1 = [[UIButton alloc] init];
    button1.frame = CGRectMake(20, 100+100, 150, 40);
    button1.backgroundColor = [UIColor lightGrayColor];
    [button1 setTitle:@"3小泽玛利亚" forState:UIControlStateNormal];
     [button1 addTarget:self action:@selector(action3) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];


    button1 = [[UIButton alloc] init];
    button1.frame = CGRectMake(200, 100+100, 150, 40);
    button1.backgroundColor = [UIColor lightGrayColor];
    [button1 setTitle:@"4波多野结衣" forState:UIControlStateNormal];
     [button1 addTarget:self action:@selector(action4) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];

}


-(void)action1{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.securityPolicy.allowInvalidCertificates = YES;
    [manager.securityPolicy setValidatesDomainName:NO];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL1]];

    NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@" 1苍井空 = %ld",downloadProgress.completedUnitCount);
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {

        NSString *pth =  [path1 stringByAppendingPathComponent:@"1苍井空.mp4"];

        return [NSURL fileURLWithPath:pth];

    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {

    }];


    //执行Task
    [download resume];





}
-(void)action2{


    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.securityPolicy.allowInvalidCertificates = YES;
    [manager.securityPolicy setValidatesDomainName:NO];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL2]];

    NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@" 2饭岛爱 = %ld",downloadProgress.completedUnitCount);
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {

        NSString *pth =  [path2 stringByAppendingPathComponent:@"2饭岛爱.mp4"];

        return [NSURL fileURLWithPath:pth];

    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {

    }];


    //执行Task
    [download resume];

}
-(void)action3{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.securityPolicy.allowInvalidCertificates = YES;
    [manager.securityPolicy setValidatesDomainName:NO];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL3]];

    NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@" 3小泽玛利亚 = %ld",downloadProgress.completedUnitCount);
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {

        NSString *pth =  [path3 stringByAppendingPathComponent:@"3小泽玛利亚.mp4"];

        return [NSURL fileURLWithPath:pth];

    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {

    }];


    //执行Task
    [download resume];
}
-(void)action4{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.securityPolicy.allowInvalidCertificates = YES;
    [manager.securityPolicy setValidatesDomainName:NO];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL4]];

    NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@" 4波多野结衣 = %ld",downloadProgress.completedUnitCount);
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {

        NSString *pth =  [path4 stringByAppendingPathComponent:@"4波多野结衣.mp4"];

        return [NSURL fileURLWithPath:pth];

    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {

    }];


    //执行Task
    [download resume];
}


-(void)createDocumentationDirectory{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    path1 = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"directoryOne"];

    if(![fileManager fileExistsAtPath:path1]){
        [[NSFileManager defaultManager] createDirectoryAtPath:path1 withIntermediateDirectories:YES attributes:nil error:nil];
    }

}
-(void)createDocumentDirectory{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    path2 = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"directoryTwo"];

    if(![fileManager fileExistsAtPath:path2]){
        [[NSFileManager defaultManager] createDirectoryAtPath:path2 withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

-(void)createDownloadsDirectory{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES);
    path3 = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"directoryThree"];

    if(![fileManager fileExistsAtPath:path3]){
        [[NSFileManager defaultManager] createDirectoryAtPath:path3 withIntermediateDirectories:YES attributes:nil error:nil];
    }

}
-(void)createCachesDirectory{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    path4 = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"directoryFour"];

    if(![fileManager fileExistsAtPath:path4]){
        [[NSFileManager defaultManager] createDirectoryAtPath:path4 withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

但是,我们还需要配置下 info.plist文件

这里写图片描述

Supports opening documents in place    =     YES
Application supports iTunes file sharing  =  YES

这里写图片描述

四个按钮对应的文件下载目录如上图所示。 。。。。

这里写图片描述

四个文件都已经下载下来。。。。。 我们去 Files文件夹中看下 。。。。

这里写图片描述

我们发现,只有第二个目录才能在Files中看得见 。。。。。

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    path2 = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"directoryTwo"];

也就是只有这个目录下的文件或者文档才能被系统Files发现,这是时候我们打开iTunes,你的手机需要连接iTunes

这里写图片描述

我们能通过iTunes管理手机APP上面的东西 。。。 我们往里面传一个文件看看 。。。

这里写图片描述

我们往里面上传了一个url.txt的文件, 我们打开 手机的文件看下

这里写图片描述

说明了一切 。。。。。。。。 这就是ios11 关于文件夹共享的总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值