Unable to access a file from code with Xcode - ios - ios

IOS development...
I have a json data file named "Data.js" in the same directory where the code file is and is trying access it with the following code...
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Data" odType:#"js"];
but this is returning null.
Any help on this is much appreciated.

The above written answers are right. By default Data.js file will be added to the compilation phase. You can drag that to the Copy Bundle Resources phase. FYI i'am adding a reference screen shot. Go to the settings page of your target under Build phases you can find these sections. Drag the selected file to Copy Bundle Resources phase

Did you check whether Data.js has been added to copy bundle resources under build phases?

When you add JS files Xcode will add them to the compilation phase (since they're code files), instead of the Copy Bundle Resources phase. You need to go to your project settings and move it to the right phase.

Related

xcode (ios/objective c) access Supporting files

Please keep in mind this is my first ios app and first experience with xcode. I have researched this but i believe my inexperience is a key factor in not being able to find it.
I am creating an app for iPhone in xcode and need help locating a file. I am currently storing an xml file in the Supporting Files of xcode. I have searched through the directories using NSDirectory trying to locate it with no success.
Any help would be appreciated.
Resources, such as an XML file, are copied into the app bundle during the build phase. You can see the list of copied files by navigating to your Project Settings, then the Build Phases tab and then expanding the Copy Bundle Resources build phase.
To find resources that are copied into the bundle you can use NSBundle to get the path to the file:
[[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"xml"];
You can then use that path to load the XML file into a an NSData or NSString object before parsing it.
The directory structure inside an iOS app is largely flat, since most files are simply copied into the bundle ignoring the group structure that represents them in Xcode. It is possible to add folder references to Xcode that will be copied to the bundle verbatim, preserving their directory structure within the bundle, (look up the differences between Groups and Folder References in Xcode for more information).

texture atlas file not found in main bundle in resources folder

I'm having a problem where a file in my Resource directory is not being recognized as being in the Main Bundle.
I am creating a texture atlas using a pvr file format using texturePacker. The output of texturePacker produces two files xxx_iPhone-hd.pvr.ccz and xxx_iPhone-hd.plist. These two files are being stored in my Resource directory of my cocos2d project. Once the files are stored in the Resource folder, I go to the Resource folder and right click to 'Add Files to "myProject"' (with the copy items into destination box unchecked).
When my cocos2d project runs and tries to load this file, the absolute path is unknown to the Main Bundle. Specifically, my code executes the following line of code:
backgroundBgNode = [CCSpriteBatchNode batchNodeWithFile:#"StoreMenuBackground_iPhone.pvr.ccz"];
The cocos2d code knows that the device has a retina display and modifies the filename to create a relPath of "StoreMenuBackground_iPhone-hd.pvr.ccz". It then tries to execute the following:
NSString *imageDirectory = [relPath stringByDeletingLastPathComponent];
fullpath = [[NSBundle mainBundle] pathForResource:file
ofType:nil
inDirectory:imageDirectory];
This results in full path being nil. I previously created a different xxx_iPhone5-hd.pvr.ccz file with the above procedure, and the above code successfully returns the fullpath. I have very carefully checked and rechecked the spelling and don't see any problems. I have also tried removing and re-adding the files under "Copy Bundle Resources" of the "Build Phases" for the project. Is there something I can do to force the mainBundle to find this file?
You should verify that the file actually is in the bundle.
You can do that by right-clicking the built bundle (in your project's DerivedData folder) and select "Show Package Contents". If you don't know how to locate the built product, do an Archive Build targeting a device (archive builds are unavailable for simulators). In the Organizer window that opens right-click the app and select "Show in Finder", that brings you to the xcarchive. Run "show package contents" on the xcarchive and navigate to /Products/Applications and perform another "show package contents" on the bundle.
If the file is there but in a subfolder (not in the root of the bundle) then you have created a folder reference inside Xcode, denoted by the blue folder icon. Remove that and re-add, this time uncheck the checkbox "create folder references for..." in the Add File dialog.
If the file IS there, copy its filename and paste it back into code. Case differences can sometimes be elusive and hard to notice, even if you double-check.
desperate measures then. Copy in finder the files on the side ... ie not in the project directory structure. Rename the newly copied files to something dead simple , like a-hd.pvr.ccz and a.plist (hoping you dont already have an a-hd.pvr.ccz in your project ;) ).
Edit a-hd.plist to make certain that the last section points to the right file name. Drag them from Finder onto the xCode resources folder, selecting to copy the files. Make certain to change the name in your code. Delete the previous files from the project. Deep clean the project. Delete the app from the device. Run. If that worked, redo the same steps, reverting the name to your favourite name.

pathForResource returns null for plist

I'm trying to get the path for a file named data.plist that is in my project. I have this code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"plist"];
NSLog(#"Path: %#",path);
That prints "Path: (null)" in the console.
The plist is in the Copy Bundle Resources build phase, so that's not the problem.
The plist is one that was pre-made for a class project, and I believe I am adding it to my XCode project properly.
If any more code snippets would be helpful in answering the question, I'll provide them.
Just because it's in the copy bundle resources build phase doesn't guarantee that it is in the correct place in your app, and that it is named correctly.
If the pathForResource:ofType: call returns nil, the file isn't in the bundle at the right place with the right name.
I suggest you do the following: Right-click on the app in the project organizer in Xcode and select "Show in finder.
Right-click on the app, open it, and examine the "contents" folder. See if your plist file is really there. Also double-check the case of both the filename and the extension.

Is Xcode's "Target Membership" related to the Bundle?

I had previously thought that if you check the box for a file to add it to a target, then that is how it gets included in the application bundle. I have some audio files that I decided not to use and I unchecked them from the Target Membership. They are also not #include or #import anywhere. I "cleaned" the Xcode project using the Product menu and also deleted its derived data. As a test I did not remove lines like this:
NSURL* file_url = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:fileName ofType:fileSuffix]];
pcmData = MyGetOpenALAudioDataAll((CFURLRef)file_url, &data_size, &al_format, &sample_rate);
Where fileName and fileSuffix refer to these files that are no longer included in the target.
However, surprisingly, these files still load and play back fine.
So apparently, files get included in an application bundle in another way besides the "target membership" checkbox. How is this happening?
The target membership determines which target a file is processed for. For instance, if your project contains both a Mac target and an iOS target, and a source file is only included in the Mac target, it will not be compiled for iOS.
A file may be a member of multiple targets. A project may contain multiple targets and generally contains at least one. A target may establish other targets as dependencies (for instance, you can make a "compile resources" target and a "build open-source library" target dependencies of your main app target.)
It sounds like the file is still present in the app bundle that is already installed on the target device or the simulator. Files in your app bundle are not deleted when the app is updated. To delete them, you must uninstall and reinstall the app (or perform another operation that deletes the app data, such as wiping the phone or resetting the simulator.)
Removing items from your target will not remove them from existing builds (wouldn't that be nice). To solve this, you can remove the application from either your device or the simulator and when you build and run again, only the files added to your target will be copied.

Identical line of code using [mainBundle pathForResource] fails in one project but works in another

In my code I want to open a html file, so added the following line of code:
NSString* path = [[NSBundle mainBundle] pathForResource:#"cheese" ofType:#"html"];
But it fails as path is nil after execution. The file cheese.html exists and has been added to the project's resources.
I couldn't, and still can't figure out why its not working. I have a few other projects that open and read files so as an experiment I copied the line of code that is failing to one of these other projects, and along with it I dragged/dropped the cheese.html file from the project where it doesn't work to this other project (dragged and dropped from within xcode's hieararchical project file view), and bingo - the line executes and path is not nil in that other project.
So I'm totally mystified, why is it working in one project but not another?
Its the exact same line of code in both, and as the file was dragged and dropped its the same file, and as the file was dragged from within xcode itself, it proves the file has been added to the projects resources. So why would it still fail?
Check this:
In the left hand organizer select your project. Then in the next navigator over Select your target name. Then Select the build phases 'tab'. You will see a section 'Copy Bundle Resources'. Make sure the resource you are looking for is listed there. If it is not add it.
Another possible solution to this problem for anyone who is experiencing it: Sometimes a file can belong to a bundle which is not the mainBundle - in this case the easiest solution is to use [NSBundle bundleForClass:[self class]] instead, to use the bundle that the current class is using.

Resources