In Xcode, the Resources folder links to the NSDocumentsFolder of the app? Or what else?
Is there a way to see the files in NSDocumentFolder without write code?
No the resource folder in Xcode does not link to the document directory, it is just imaginary folder for organization in xcode. However there you could see the content of your document directory by browsing to the folder in the simulator.
The folders you use in XCode will not be copied to your application bundle even though all their content (as well as anything else you have in your XCode project...excluding compiled resources) will be flatly copied into your Bundle Folder. If what you need is accessing the Documents folder on an iOS app or in a the User's library you can access it (by code, I'm sorry) with:
NSString *documentFolderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
And anything you need to be in there you will have to copy by code.
Related
I have a phonegap application where it has added a cocoa touch static library project to it as a sub project. I need to access an xml file that is embedded to the library project.
NSString *configXmlFilePath=[[NSBundle mainBundle]pathForResource:#"configurationFile" ofType:#"xml"];
NSString *xmlContent=[[NSString alloc]initWithContentsOfFile:configXmlFilePath encoding:NSUTF8StringEncoding error:nil];
that's how I access a file in the project usually. but since this is a sub project, I can't access the path like this.Does anyone know how to do this?
You should be able to drag the file in question from your subproject in the Project Navigator to the "Copy Resources" build phase in your master Xcode project. Let me know if you'd like me to explain in detail.
I'm trying to obtain the path for a java file which I want to load into an NSString.
Currently this line of code is returning nil.
[[NSBundle mainBundle] pathForResource:#"LockDialog" ofType:#"java"]];
I added my file "LockDialog.java" to the project via the menu, File>New>New File. The pathForResource method seems fine for returning the path of txt files or html files but completely fails when I'm trying to get the path of a java file.
Any help or insight massively appreciated,
Thanks,
James
File type should not matter, but chances are the file is not actually being copied to your application bundle. Check your Target>Build Phases>Copy Bundle Resources settings to ensure it's there, and if not, drag it over to this list.
In addition to adding the file to the project, you need to make sure that it gets added to the output bundle.
Open the project in Xcode, navigate to your target, switch to the "Build Phases" tab, and add "LockDialog.java" to the "Copy Bundle Resources" list.
I have manually created a property list (Config.plist) using Xcode 4.3 and saved it in "Supporting Files/Config.plist".
However, when building the project and running the app in the simulator, the property list is not copied in the document directory (as supposed) nor any other directory in the simulator.
Any help would be highly appreciated.
Thank you!
It will go to NSBundle mainBundle by default. You will require to copy that file to document directory. Check my code for copy file to document directory over here:
Plist
I've been teaching myself over the last couple of weeks by typing in programs from the iPad books I bought (Backlin's, SAM's, Apps for Dummies, etc.) and YouTube tutorials. Still, there are a couple of things I haven't grasped intuitively. Do you mind helping me out?
I got a program working that saves and retrieves names and phone numbers from pList files. I looked on the Mac's HD and couldn't find the file (Contacts.plist), even though the program was working. I finally discovered it at
~/Library/Application Support/iPhone Simulator
I'm not sure why Finder didn't locate it. My own app needs to load a data file when it starts (questions and answers, for example). Do I write a program to create the Q & A file, (I could modify that phone program to do that) then copy the file into the simulator's virtual directory for my own app? Or do I copy that file into the XCODE Resources folder? Would the data files then travel with the finished executable program?
Sorry to sound so clueless. Thanks for any info. -Rob
If you want to deliver a file with your finished application, you must add it to your Xcode project. It doesn't matter whether you place it under Resources or in another group in Xcode as Xcode will by default copy all non-code files that are in your project into your finished app bundle.
To access this file from your code, you need to retrieve its path:
NSString *fullPathToContactsFile = [[NSBundle mainBundle] pathForResource:#"Contacts" ofType:#"plist"];
Note that unlike on the iOS Simulator, your app bundle on the device is read only, so if you want your app to make changes to the file, you cannot save it in the bundle itself. In that case, your app should copy the file from your bundle to your app's Documents or Library directory on first launch and then open/save it from/to that location.
I'm having a really weird problem with the iPad app I'm writing. On startup I want to copy a folder containing a few other folders (that are empty) from the application bundle to the Documents directory. The folder to be copied, called 'flds' (all lowercase), was added to the Xcode 4 project using 'Create folder references for any added folders', and I have checked that it is actually part of the .app file after compiling. (And that it doesn't exist already when attempting to copy.)
I've tried getting to the 'flds' folder using either one of these calls (they all work):
[[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:#"flds"]
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"flds"]
[[NSBundle mainBundle] pathForResource:#"flds" ofType:#""]
Then copying using NSFileManager's copyItemAtPath:toPath:error: method (or the URL equivalent when using URL instead of path string).
All of these strategies work perfectly in the iPad Simulator and on the iPad device, with the following exception: When I (successfully) build for Ad Hoc distribution, drag the .app and the .mobileprovision into iTunes, sync and then run the app on the device, the system no longer thinks the 'flds' folder exists! I've been trying to examine this for many hours, with no luck. Again, I'm perfectly sure it works both in the simulator and on the device running from Xcode, but not when synced via iTunes.
Ideas?
I discovered that the following call gets the folder path successfully also after syncing the iPad app via iTunes:
[[NSBundle mainBundle] pathForResource:#"flds" ofType:nil inDirectory:nil]
However, when I use this path to copy the folder to its new location in the Documents directory, only the folder itself and the .txt file in it (which I put in there as a test, it's not really supposed to be there) gets copied, and not the 6-7 empty subfolders. Again, the same pattern shows itself: Works as expected both on simulator and device running from Xcode ('flds' folder with 'test.txt' and empty subfolders are copied successfully), but not when synced via iTunes (only 'flds' folder with 'test.txt' are copied, not subfolders). Is this really the intended behaviour of copyItemAtPath:toPath:error:? And why would it behave differently after syncing via iTunes?
In the end I figured out that copyItemAtPath:toPath:error: wouldn't copy empty folders, even though I think it's supposed to. And again, this behaviour only occured after syncing the app to the iPad via iTunes (when running from Xcode it did copy empty folders). My solution for the time being is to simply put a dummy text file in each subfolder, so that they're not empty anymore.