iOS获取沙盒目录+URLForDirectory:inDomain:appropriateForURL:create:error: NSFileManager

参考:

1. http://stackoverflow.com/questions/13983091/nsfilemanager-urlsfordirectory-or-urlfordirectory

2. http://blog.csdn.net/enuola/article/details/8076221

目的:

其实参考里已经介绍了获取沙盒路径方法,但是我很想知道的是用NSUserDomainMask时,使用不同NSSearchPathDirectory 我的iOS返回是什么?

Environment: Mount Lion ,Xcode 4.5.3 , cocos2d-iphone V2.0


NSSearchPathDirectory值实际ios返回值
NSApplicationDirectory = 1file://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Applications
NSDemoApplicationDirectoryfile://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Applications/Demos
NSDeveloperApplicationDirectoryfile://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Developer/Applications
NSAdminApplicationDirectoryfile://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Applications/Utilities
NSLibraryDirectoryfile://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Library/
NSDeveloperDirectoryfile://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Developer
NSUserDirectorynil
NSDocumentationDirectoryfile://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Library/Documentation
NSDocumentDirectoryfile://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Documents/
NSCoreServiceDirectorynil
NSAutosavedInformationDirectory = 11file://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Library/Autosave%20Information
NSDesktopDirectory = 12file://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Desktop
NSCachesDirectory = 13file://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Library/Caches/
NSApplicationSupportDirectory = 14file://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Library/Application%20Support
NSDownloadsDirectory = 15file://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Downloads
NSInputMethodsDirectory = 16file://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Library/Input%20Methods
NSMoviesDirectory = 17file://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Movies
NSMusicDirectory = 18file://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Music
NSPicturesDirectory = 19file://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Pictures
NSPrinterDescriptionDirectory = 20nil
NSSharedPublicDirectory = 21file://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Public
NSPreferencePanesDirectory = 22file://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Library/PreferencePanes
NSItemReplacementDirectory = 99nil
NSAllApplicationsDirectory = 100file://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Applications
NSAllLibrariesDirectory = 101file://localhost/Users/hemarcus/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/00F96338-26D1-491D-8BA7-E97663752A1F/Library/
那么NSSearchPathDomainMask不是NSUserDomainMask时能使用urlForDirectoryxxx方法吗??iOS下有啥不同??

NSSearchPathDomainMaskURLForSearch=NSLibraryDirectoryURLForSearch=NSApplicationDirectoryURLForSearch=NSDemoApplicationDirectory
NSLocalDomainMask = 2file://localhost/Library/file://localhost/Applications/file://localhost/Applications/Demos
NSNetworkDomainMask = 4file://localhost/Network/Libraryfile://localhost/Network/Applicationsfile://localhost/Network/Applications/Demos
NSSystemDomainMask = 8file://localhost/System/Library/file://localhost/Applications/file://localhost/Applications/Demos
NSAllDomainsMask = 0x0ffff同NSUserDomainMake返回值同NSUserDomainMake返回值同NSUserDomainMake返回值

  • 0
    点赞
  • 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、付费专栏及课程。

余额充值