简体   繁体   English

如何在iOS中以编程方式访问应用程序目录

[英]How to access app directory programmatically in iOS

If we create a database file programmatically, it will store in somewhere in /Users/admin/Library/Application Support/iPhone Simulator/5.1/Applications/DB425A55-5008-44D2-B08D-C21AFFBAF935/Documents . 如果我们以编程方式创建数据库文件,它将存储在/Users/admin/Library/Application Support/iPhone Simulator/5.1/Applications/DB425A55-5008-44D2-B08D-C21AFFBAF935/Documents For this, we have to use below code. 为此,我们必须使用以下代码。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"sample.db"];

Is there any way to create and store database files in our local app folder? 有什么方法可以在我们的本地应用程序文件夹中创建和存储数据库文件?

You're thinking about your app's bundle - you can get details about this by reading the documentation for the NSBundle class. 您正在考虑应用程序的捆绑软件-您可以通过阅读NSBundle类的文档来获取有关此信息的详细信息。

However, you only have read access to this - you can read the contents of file in the bundle but you can't change them etc. This is beacuse your bundle is signed and if you were able to change it then the certificates checksums etc wouldn't work :) 但是,您仅对此具有读取权限-您可以读取捆绑软件中的文件内容,但不能更改它们等。这是因为您的捆绑软件已签名,如果您能够对其进行更改,则证书校验和等不会不工作:)

Why do you specifically want to write into your app's folder? 您为什么特别想写入应用程序的文件夹? What's wrong with the other directories provided for your app? 为您的应用程序提供的其他目录怎么了?

不可以,您不能进行更改/添加到您的应用程序捆绑包中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM