Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
760 views
in Technique[技术] by (71.8m points)

cocoa touch - Is an iPhone app's document directory /var/mobile/Documents or /var/mobile/Library/AppName?

As far as I know (and read everywhere) an application's documents directory should be somewhere in /var/mobile/Library/ on the iPhone but if I'm logging the gotten directory it is /var/mobile/Documents.

I get the directory as follows:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

is this a problem if I'm creating a file named Settings.xml in here or is something wrong with my app/etc.?

Information: I've a jailbroken iPhone 4 with iOS 4.2.1 and my app gets installed via Cydia or SSH.


Update

Yeah, but that directory is not really related to my application. For example I am creating a document settings.xml and what if another application uses the same NSDocumentDirectory and the same file name? And yes, I know. Cydia apps got installed into /Applications but there should be a document directory for my app too and not even /var/mobile/Documents. If you take a look at http://thebigboss.org/hosting-repository/submit-your-app/compile-for-cydia-submission the directory should be /var/mobile/Library/ but as I described before, I never get this directory.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using the constant NSDocumentDirectory will in fact get you /var/mobile/Documents when running a jailbreak app. It would be bad policy to dump your app's documents directly in there, even if you think they're named uniquely.

If you look at the contents of the /var/mobile/Library/ folder, you'll see that many apps have their data stored there, including jailbreak apps. It's not clear to me whether there's a handy constant like NSDocumentDirectory that you can use to get that. But, you can always hardcode your source to look for files in @"/var/mobile/Library/APPNAME". It's unlikely that this folder will change (and if it does, it's probably going to break other apps, and is certainly an easy fix to make).

If you look at the document you link to, it does specify:

Appstore app has a Documents folder that is created by the installation process. Jailbroken app does not. It is up to the app to create its own folder. Should you need this type of folder, you must create this with a simple mkdir command in your applicationDidFinishLaunching function. Just add a simple function: mkdir(“/var/mobile/Library/YOURAPPNAME”, 0755); If the folder already exists, no harm done. You want to do this because the install process runs as user root and the app runs as user mobile. If Cydia does this for you then the folder will have the incorrect permissions.

... so you need to create that subdirectory yourself. It won't be there if you don't.

In my experience, writing jailbreak apps simply involves some of these slightly stinky coding practices (hardcoding paths and creating them ourselves) ... it might just be something we have to deal with.

Update: make sure to see Saurik's comment below.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...