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 关于文件夹共享的总结

### 设置和使用Windows共享文件夹 #### 创建并配置共享文件夹 为了使办公室内的多个用户能够方便地共享文件,在一台作为主机的计算机上创建一个专门用于共享文件夹是必要的。具体操作如下: 选择目标磁盘分区,新建或选取已有文件夹,右键点击该文件夹,进入【属性】选项卡中的【共享】子项[^2]。 在弹出窗口中启用共享功能,并可自定义共享名称以便于识别。此时,其他设备可以通过网络路径访问此文件夹及其内容。 #### 获取主机IP地址与用户名 对于希望连接到上述共享资源的客户端而言,了解提供服务端机器的具体位置至关重要。这通常意味着要知道其在网络上的唯一标识——即IP地址以及登录凭证(如果设置了权限验证)。获取这些信息的方法包括但不限于命令提示符下的`ipconfig`指令查询本地IPv4地址;而用户名则取决于操作系统安装时所设立或是后来修改过的账户详情[^3]。 #### 调整防火墙及其他安全设置 考虑到网络安全因素,默认情况下某些防护机制可能会阻止外部尝试建立连接的行为。因此有必要确认防火墙允许来自内部网络的数据包通过,特别是针对Microsoft网络客户端和服务的相关规则应处于开启状态。此外,还需确保工作组模式下各成员间的信任关系已妥善处理,避免因认证失败而导致无法正常读写数据的情况发生[^1]。 #### 实现跨平台访问 当涉及到不同类型的终端设备如iOS移动装置时,则需额外注意兼容性和协议支持度等问题。例如iPhone可通过SMB/CIFS协议来浏览由Windows PC发布的共享目录,但前提是两者必须位于相同逻辑网段内,并且按照指示完成相应应用程序层面的操作流程,比如利用Files应用里的“连接服务器”特性指定UNC路径格式(`smb://<hostname_or_IP>/<sharename>`)进行挂载。 #### 文件同步解决方案 除了简单的即时存取外,有时还存在定期备份或者保持多份副本一致性的需求。这时可以考虑借助第三方工具实现更高级别的自动化管理,或者是基于云服务平台提供的API接口开发定制化脚本来达成目的。不过就最基础的功能来说,只要保证所有参与者都能稳定接入同一个物理/虚拟存储空间即可满足大部分场景的要求[^4]。 ```powershell # PowerShell示例:快速显示本机IPV4地址 (Get-WmiObject Win32_NetworkAdapterConfiguration | Where {$_.IpEnabled}).IPAddress ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值