计算ios沙盒中的文件大小

 [代码]计算缓存大小 沙盒下得文件    跳至 [1] [全屏预览]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-( float )fileSizeForDir:( NSString *)path //计算文件夹下文件的总大小
{
     
     NSFileManager *fileManager = [[ NSFileManager alloc] init];
     float size =0;
     NSArray * array = [fileManager contentsOfDirectoryAtPath:path error: nil ];
     for ( int i = 0; i<[array count]; i++)
     {
         NSString *fullPath = [path stringByAppendingPathComponent:[array objectAtIndex:i]];
         
         BOOL isDir;
         if ( !([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && isDir) )
         {
             NSDictionary *fileAttributeDic=[fileManager attributesOfItemAtPath:fullPath error: nil ];
             size+= fileAttributeDic.fileSize/ 1024.0/1024.0;
         }
         else
         {
             [ self fileSizeForDir:fullPath];
         }
     }
     han = han + size;
     return size;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 上,应用的沙盒目录是私有的,其他应用无法直接访问。因此,要从 JS 获取 iOS 沙盒内的文件,需要通过 Native Bridge 的方式,即将 JS 的请求传递给原生代码,由原生代码来实现文件读取并返回结果给 JS。 以下是一个简单的示例,假设你要获取沙盒内的一个名为 `example.txt` 的文件: 1. 在原生代码实现文件读取逻辑,例如使用 `NSFileManager` 类读取文件内容: ```objc - (NSString *)readFileContentsAtPath:(NSString *)path { NSFileManager *fileManager = [NSFileManager defaultManager]; NSData *data = [fileManager contentsAtPath:path]; return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; } ``` 2. 在 JS 定义一个函数,用于调用原生代码并获取文件内容: ```js function getExampleFileContents() { // 调用 Native Bridge 方法,传递路径参数 const path = 'path/to/example.txt'; const contents = NativeBridge.readFileContentsAtPath(path); return contents; } ``` 3. 在原生代码实现 Native Bridge 方法,并将其暴露给 JS 环境: ```objc - (void)registerNativeBridgeMethods { [self.bridge registerNativeMethod:@"readFileContentsAtPath" handler:^(NSString *path) { NSString *contents = [self readFileContentsAtPath:path]; // 将结果返回给 JS 环境 [self.bridge callHandler:@"onFileContentsReceived" withArguments:@[contents]]; }]; } ``` 4. 在 JS 注册 `onFileContentsReceived` 回调函数,用于接收原生代码返回的文件内容: ```js function onFileContentsReceived(contents) { // 处理文件内容 console.log(contents); } NativeBridge.registerEventHandler('onFileContentsReceived', onFileContentsReceived); ``` 注意:以上示例仅为演示 Native Bridge 的基本用法,具体实现方式可能因框架、平台、业务需求等因素而异。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值