获取App各种文件的路径方法汇集

iOS文件路径获取
本文介绍了在iOS开发中如何使用Objective-C获取各种类型的文件路径,包括Sandbox路径、Documents路径、Library路径及其Caches目录、临时目录及应用程序自身的路径。
[super viewDidLoad];
    // 获取App的Sandbox路径
    NSString *sandboxPath = NSHomeDirectory();
    NSLog(@"%@",sandboxPath);
    //获取Documents路径
    NSString *doc = [sandboxPath stringByAppendingPathComponent:@"Documents"];
    NSLog(@"doc:%@",doc);
    //另一种获取Documents路径的方式
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *doc1 = [paths firstObject];
    NSLog(@"Doc:%@",doc1);
   
    //获取Library的路径
    NSString *lib = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
    NSLog(@"%@",lib);
    
    //获取Library下的Caches目录
    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];
    NSLog(@"Caches:%@",caches);
    
    //获取临时目录下tmp
    NSString *tmp = NSTemporaryDirectory();
    NSLog(@"tmp:%@",tmp);
    
    //如何获取应用程序本身的路径
    NSBundle *mainBundle = [NSBundle mainBundle];//mainBundle 对象代表应用程序本身的内容
    NSString *path = [mainBundle pathForResource:@"a" ofType:@"jpg"];
    NSLog(@"%@",path);
    //获取Url
    NSURL *url = [mainBundle URLForResource:@"a" withExtension:@"jpg"];
    NSLog(@"url:%@",url);


转载于:https://my.oschina.net/iOSBoyJack/blog/521478

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值