How to access local files within project - iphone

I want to add some static files (image, binary, etc...) to my app. I've placed them under a folder named Resources and have added it to my XCode project.
Next, I have those files added to the Copy Bundle Resources in the Build Phases tab in the project settings.
However, I can't seem to refer to them in the right way.
For instance, reading a binary file:
NSInputStream *is = [[NSInputStream alloc] initWithFileAtPath:#"data.bin"];
if(![is hasBytesAvailable]) NSLog(#"wrong");
This always fails & prints wrong
I tried a similar approach with NSData initWithContentsOfFile: but this wouldn't even execute. Another attempt was to use [NSBundle mainBundle] but this also didn't execute fully.
I'm new to iOS please help I can access my local files! Any help would be appreciated.
Thank you,

You need to ensure that you're accessing the file in the right place. Xcode places your resource files in the application's "application bundle" in the Resources directory. There are many ways to dig 'em out.
A common way is to get the pathname of a file in your bundle:
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *myFile = [mainBundle pathForResource: #"data" ofType: #"bin"];
If you have other files in nonstandard places in your bundle, there are variations to pathForResource that let your specify a directory name.
If you want to see the actual location on your hard-drive that the simulator is using so you can inspect it, you can say:
NSLog(#"Main bundle path: %#", mainBundle);
NSLog(#"myFile path: %#", myFile);
Search for the "Bundle Programming Guide" in the Xcode documentation library to get started. :-)

Use NSBundle class for that. For Example:
NSString *path = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"bin"];
NSInputStream *is = [[NSInputStream alloc] initWithFileAtPath:path];
...

Indeed calling [is open] is needed even though you "initialiseWithFileAtPath". I would think the initialisation would automatically open the file for you, but it doesn't.

Related

sqlite db path in bundle acces?

I want to punt my sqlite db in to a custom bundle and use that bundle in more than one project.
I have created a new target from Mac OS X FrameWork&Library -> Bundle, I have build the target and the I have put my sqlite db in the bundle.
I have added the bundle to my iOS project and try to read the db from it.
Now comes the problem: I can get the bundle and read its identifier but when I try to get the path for db I get null return string.
The code to read db path:
NSBundle *myDbBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle]pathForResource:#"myDbBundle" ofType:#"bundle"]];
NSLog(#"BundleIdentifier:%#",[myDbBundle bundleIdentifier]);
NSString *databasePath = [myDbBundle pathForResource:#"myDbName" ofType:#"sqlite"];
NSLog(#"DataBasePath:%#",databasePath);
Any sugestion, docs or tutorial about this issue are wellcome.
Update:
It seems that I can not load the bundle that is why when I try to read from it I fail.
Code:
NSError *error;
NSLog(#"LoadBundle:%#",[myDbBundle loadAndReturnError:&error]?#"yes":#"no");
NSLog(#"Error:%#",[error localizedDescription]);
So the real problme is how do I create a bundle in witch I place a sqlite db and witch can be read from ios app? To create the bundle I used the xcode template under Mac OS X so I think that I have to change something in the build settings, but I don't know what.
NSString *databasePath = [[NSBundle mainBundle] pathForResource:#"myDbName" ofType:#"sqlite"];
should be good enough to get the path to your myDbName.sqlite inside your main bundle
EDIT:
Okay, here is what I did, first, New File -> Resources -> Settings Bundle, create a new one, name it myDbBundle, then right click on that newly created bundle, add myDbName.sqlite in it, make sure that it's not inside en.lproj. Also, make sure when you create a new bundle, you tick the targets to include it into your current target, since this is not Settings.bundle, you want to include it. Then run you code, it works fine.
and here is the code I use
NSBundle* bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:#"myDbBundle" ofType:#"bundle"]];
NSLog(#"%#", bundle);
NSString* test = [bundle pathForResource:#"myDbName" ofType:#"sqlite"];
NSLog(#"%#", test);
NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:[bundle pathForResource:#"Root" ofType:#"plist"]];
NSLog(#"%#", dict);
the NSLog(#"%#", dict); is printing out correct value, which means it's successfully loaded
I haven't done this myself, but this blog seems to be what you are trying to do Universal framework iPhone iOS 2.0
It describes creating a framework that can be used to package up any files and then simply imported into other projects as needed.
Swift 3
let dbname = Bundle.main.path(forResource: "horoscope-release", ofType: "sqlite")!

How to get file path in iPhone app

I have a problem accessing my files in my app.
I am currently using
//Directly from TileMap example from WWDC2010
NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"Tiles"];
to access my tiles for my MKOverlay. This gives me this directory
/Users/xxxx/Library/Application Support/iPhone Simulator/4.2/Applications/9D62025C-C53B-472C-8309-xxxx/xxxx.app/Tiles
The x's is only for privacy reasons
I have my tiles in a folder called Tiles in the root of my application which is in Xcode in a group called Tiles which is in directly in the Resources group.
When I run my app, I get a simple error saying that it could not find my tiles at the generated directory (the one quote above) If I replace that piece of code and make it:
NSString *tileDirectory = #"/Users/xxxx/Documents/xxxx/Tiles";
Then my app works fine. This is obviously because it finds my tiles in its direct location on my Mac. This is fine for testing, but I need it to work on my iPhone/iPad.
This problem might be occurring due to:
The generated directory is incorrect.
The tile images aren't getting included in the builded .app file.
Either way, I have no clue of what to do to solve it.
How can I solve this problem?
[EDIT]
I changed that piece of code to:
NSString *tileDirectory = [[NSBundle mainBundle] resourcePath];
Now it works in simulator, because all files are in the apps root folder and I don't ask for it to enter another directory called "Tiles".
This runs with no error on the simulator, but when on my iPhone it gives the original error (just a different file path but also ending with /xxxx.app
How can I ensure a directory in my app file such as xxxx.app/Tiles - TileMap does this.
Since it is your files in your app bundle, I think you can use pathForResource:ofType: to get the full pathname of your file.
Here is an example:
NSString* filePath = [[NSBundle mainBundle] pathForResource:#"your_file_name"
ofType:#"the_file_extension"];
Remember that the "folders/groups" you make in xcode, those which are yellowish are not reflected as real folders in your iPhone app. They are just there to structure your XCode project. You can nest as many yellow group as you want and they still only serve the purpose of organizing code in XCode.
EDIT
Make a folder outside of XCode then drag it over, and select "Create folder references for any added folders" instead of "Create groups for any added folders" in the popup.
If your tiles are not in your bundle, either copied from the bundle or downloaded from the internet you can get the directory like this
NSString *documentdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *tileDirectory = [documentdir stringByAppendingPathComponent:#"xxxx/Tiles"];
NSLog(#"Tile Directory: %#", tileDirectory);
You need to use the URL for the link, such as this:
NSURL *path = [[NSBundle mainBundle] URLForResource:#"imagename" withExtension:#"jpg"];
It will give you a proper URL ref.
You need to add your tiles into your resource bundle. I mean add all those files to your project make sure to copy all files to project directory option checked.

(IPHONE) Big confusion to read/write the same file from resource folder

i've read several thread for this question but unfortunatly not found the answer to my problem :(
i have a xml file in resource folder, and i need just to re-write the same file;
the app logic is:
display data loaded from file
add new data to the same file
for read a file my code is:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"myfile" ofType:#"xml"];
NSData *dataXml=[NSData dataWithContentsOfFile:filePath];
for re-write the same file:
NSString *myDataString = #"?xml version=""1.0"" encoding=""UTF-8""?";
NSString *path = [[NSBundle mainBundle] pathForResource:#"myfile" ofType:#"xml"];
BOOL ok=[myDataString writeToFile:path atomically:YES encoding:NSUnicodeStringEncoding error:nil];
For read works fine, but when i try to write, the file is not updated, but it was created another one in another folder, i've tested this in the simulator noting that an app is in a folder like this:
Users/user/Library/Application Support/iPhoneSimulator/Users/Applications/05279766-7B2C-4FC0-BFB0-D87A158A3337/
where there are 3 folder (Documents,tmp,Libary) and the bundle file .app (where there is my xml file)
My question are: is my logic wrong? in wich way are managed app folders in the device??
thanks in advance and sorry for the long thread!
You cannot write files into your application bundle; it's read only (on the device, at least; you may be able to do this in the Simulator).
What you should do is use your ~/Documents folder for the file. If it's not there, first read it from your bundle, then write it out to ~/Documents, and from then on, read it from there.

Problem getting path to .plist using [[NSBundle mainBundle] pathForResource

I'm an Objective C noob, and I don't know enough to explain the following problem.
This code works:
NSString *plistPath = #"/Users/andrewf/MyApp/Resources/Plates.plist";
dicPlates = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
My dictionary object is loaded with values as expected.
This code does not work:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"Plates" ofType:#"plist"];
dicPlates = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
plistPath comes back with a value of nil. This is the case irrespective of whether I include inDirectory:#"Resources" in the call or not. All the examples that I have found do not include inDirectory when trying to open a .plist file in the Resources directory.
I have confirmed that the file exists in the correct location and even recreated it to be sure.
This seems like such a simple problem, but I am mystified. Please assist.
I think you're confused as to where pathForResource: is looking.
This works:
NSString *plistPath = #"/Users/andrewf/MyApp/Resources/Plates.plist";
Notice that this path does not point to your application. It points to your project directory. Your plist is supposed to be at #/Users/andrewf/MyApp/build/Release/MyApp.app/Resources/Plist.plist" (for an iPhone app this would be different, more like #"/Users/andrewf/Library/Application Support/iPhone Simulator/User/Applicatons/(unique id)/MyApp.app/Resources/Plates.plist"), and it is inside the Resources folder of your app that pathForResource: is looking.
What this implies is that your resource is not in the "Copy Bundle Resources" phase of your build target. You need to drag it from the Groups and Files area to inside that phase.
I have found the problem!
The .plist file was included in the target correctly.
When the .plist was originally created, I named the file "plates.plist". Immediately after creating the file, I renamed it to "Plates.plist", and this is what I used henceforth.
Even though the file was named "Plates.plist" in my resources folder, in the target section and in the iPhone Simulator location mentioned above, the file was named "plates.plist". Curiously, the contents of the file was still updated correctly.
Now that I have changed the code to refer to "plates.plist", and renamed the file in the Resources folder to the same for good measure, everything works. I can only assume that this is a bug in the iPhone SDK.
Check that the plates.plist file has been added to the target in Xcode.
If the file is not added to the target it will not be inside the build product and NSBundle will not find it.

Bundles and file access

I've adding a series of folders to my project (using the Add existing file option). This results in the folder contents being shown in Xcode as a blue folder.
The following works:
NSString *imageName = [NSString stringWithFormat:#"/File-03/images/image-%02d.jpg", imageIndex];
return [UIImage imageNamed:imageName];
However, I want to switch to using imageWithContentsOfFile so the images are not cached.
The code below returns nil for the image:
return [UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"/File-03/images/image-%02d.jpg", imageIndex]];
I also tried accessing the imageWithContentsOfFile using a bundle, no go.
What am I missing here?
To access and store files on the iPhone I recommend reading this.
Erica Sadun has also written a nice shorty about accessing file within a app bundle.
Note that how files are organized in Xcode does not show how they will end up in the app bundle. The best way is to select the app bundle in the Finder and CTRL-click and select Show Package Content.
Okay, I figured out one way in code to get what I needed:
NSString *dir = [NSString stringWithFormat:#"File-03/images", imageIndex];
NSString *file = [NSString stringWithFormat:#"image-%02d", imageIndex];
NSString *path = [[NSBundle mainBundle] pathForResource:file ofType:#"jpg" inDirectory:dir];
return = [UIImage imageWithContentsOfFile:path];
The trick was to inDirectory in the bundle call.
The problem with your imageWithContentsOfFile code is that you're assuming that / is the root of the bundle, when it's the root of the FS!
So, pathForResource:ofType:inDirectory: is probably the easiest way to get the path you need.
Otherwise, you can get the root of the bundle and prefix that to your paths.