How to get file path in iPhone app

124,130

Solution 1

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.

Solution 2

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"];

Solution 3

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);

Solution 4

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.

Share:
124,130
Pangolin
Author by

Pangolin

Updated on November 07, 2020

Comments

  • Pangolin
    Pangolin over 3 years

    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.

  • Pangolin
    Pangolin almost 13 years
    You are definitely on the right track! (Thanks) How on earth do I ensure a directory in my Target as Tiles ? TileMap does it, but I can't seem to see how Apple did it there...
  • Pangolin
    Pangolin almost 13 years
    Works perfectly! Thanks! PS: Now I also understand that Recursive-Groups/Folder-references better. Thanks!
  • Dominic K
    Dominic K over 12 years
    Ugh thanks for this, I spent half an hour with file paths before stumbling upon this answer and hitting myself in the head about these file 'groups' I created in Xcode that aren't real folders...
  • themihai
    themihai about 7 years
    How do you get it by reference?
  • iOS Developer
    iOS Developer almost 6 years
    if my files are not in the bundle but in ionic project that has ios and android plateform. so in a class of ios folder i am mentioning files so how can i do that, i tried this gives errors