iPad Text File Importing - objective-c

I'm trying to import a text file into my iPad application using the method initWithContentsOfFile:encoding:error:, but when I try to get the filepath to my text file, I am running into some troubles. It seems that my text file is not being found. The following call returns nil (the text file is called legal.txt)
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"legal"
ofType:#"txt"];
Obviously, after this returns nil, my call...
NSString *fileContents = [[NSString alloc] initWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:#"error"];
also returns nil.
I am quite new at this so I might be doing something obviously incorrect, but looked all over SO and elsewhere and couldn't find anyone with the same problem as me.

Call pathForResource:ofType:inDirectory:
by passing the directory as well

Look in your app target's Copy Resources build phase and make sure legal.txt is making it into your app bundle. You can also look inside the compiled app package in your build folder (~/Library/Developer/Xcode/DerivedData/AppName-.../ by default in Xcode 4).

Related

Load files from folder

I'm loading a folder with files using this method:
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:#"GBC/Caras/"];
fotosJugadores = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: #"/Users/exular/Developer/GBC/exular/GBC/Caras/" error: &error];
But the problem remains to get the same functionality to do it once the app will run in the device, how can I set a folder with all that images to be available in the device? Is there any bundle method similar to contentsOfDirectoryAtPath: to load images from project bundle, something like imageNamed: ?
thanks
Is there any bundle method...
When you start a question like that, the answer is usually found in the NSBundle class reference. In this case, the answer is yes, there is such a method. You will see that if you read the documentation. You should always check the documentation before asking for help.

how to get the full path of an ios application? [duplicate]

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 4 iOS5 how to include a text configuration file with the app?

I know how to read/write to local files on iOS using file handles. I can dynamically create and read/write form them.
What I'm trying to do right now is to include a file, about 200 lines long with the app bundle, which I hope to parse and extract configuration settings from. I really do not want to have to create an array of 200 numbers by hand.
How would I go about including a text file with the app, and making it accessible from my application?
Thank you!
You can include a file in the bundle by adding it to your Resources folder in Xcode and making sure that it is included in your target as well. To access a file in the bundle (e.g., a text file called info.txt), you can get the path using:
[[NSBundle mainBundle] pathForResource:#"info" ofType:#"txt"]
Then access it using the normal NSFileManager methods.
EDIT:
With this said, you can't write anything into this file inside the bundle on iOS. Instead, you can copy the file from the bundle to a file system location (say, your app's Application Support folder) on first launch, then access it there elsewhere in your app.
Why don't you create a dictionary with Property List Editor and load it like
NSString *configurationPath = [[NSBundle mainBundle] pathForResource: #"configuration" ofType: #"plist"];
NSDictionary *configuration = [NSDictionary dictionaryWithContentsOfFile: configurationPath];
So you can access its settings with
[configuration objectForKey: #"someSetting"];
If you want to write, I recommend registering that dictionary with NSUserDefaults so you could write to a copy like
[[NSUserDefaults standardUserDefaults] setObject: someSetting forKey: #"someSetting"];

How do I load a string table from outside application bundle on iOS?

I would like to know how to load a resource string table from a file in application documents and not from application resource bundle or any other bundle.
Do you mean something like
NSString *path = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"fileExtension"];
that will get you the path to the file fileName.fileExtension from the app bundle...
There is NSLocalizedStringFromTableInBundle method which means you would have to put the string table in a NSBundle. Theory would be that you could download this NSBundle from the internet. Before you load your MainView Controller, you would load a controller that would check to make sure it has the latest version in NSApplicationSupportDirectory.
This NSBundle cannot contain any binary code because it's against Apple App Store policy to download code in any way or form.

Objective C: Reading text files

I've done this before, but its not working for me now. I'm doing:
NSString* path = [[NSBundle mainBundle] pathForResource:#"test"
ofType:#"txt"];
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSLog(#"%#",path);
and it returns (null) every time when I NSLog path and content. Can anyone see what I'm doing wrong?
content will be nil (which logs as '(null)') if you pass it a path it can't open. So your only issue is that the relevant instance of NSBundle is unable to find test.txt within the resources part of your application bundle.
You should:
check the file is in your Xcode project; and, if it is,
check it's included in the 'Copy Bundle Resources' phase underneath your selected Target (in the project tree view on the left in the normal Xcode window layout) and, if it is,
look inside the generated application bundle (find your product, right click, select 'Reveal in Finder', from Finder right click on the app and select 'Show Package Contents', then look for your file in there) to make sure that it's there.
If it's copied in but the relevant instance of NSBundle can't find it then something very strange is afoot.