This is driving me crazy as I cannot figure out what in the world is going on. I load up files form you main bundle all the time, xml files, html files, etc. But, now I am trying to get the contents of a javascript file but it can never find it. I am using:
NSData *jsData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"global" ofType:@"js"]];
if (jsData) {
NSLog(@"%@", jsData);
} else {
NSLog(@"Can't find file");
return;
}
Even checking the [[NSBundle mainBundle] pathForResource:@"global" ofType:@"js"] string returns null.
My globaly.js file is in my Resources folder, the exact location where my other files are location that work totally fine using the above method.
Why can't it find my js file?
Javascript files are stored on the server. They're sent to the browser the same way HTML, CSS and image files are sent.
Right-click the file and select "Open With" and "Notepad." This will open Notepad with your JS file loaded. Since JS files are always written in plain text, Notepad is a suitable editor for the file. When you're finished editing, go to "File" and "Save" to save the file.
A JS file is a plain text file that contains JavaScript code. It is used to execute JavaScript instructions in a webpage.
Most likely, the problem is that you are including your js file in a head tag or somewhere above your main content. By doing this, you are executing your js code before full html content has been attached.
By default Xcode may be treating files with the "js" extension as source files, and adding them to its Compile Sources build phase rather than its Copy Bundle Resources build phase.
Just move your "js" files to the Copy Bundle Resources build phase and they'll be copied into your application's bundle. Whether they're in a group named Resources in the project hierarchy is immaterial; that's just an organizational tool, it has no meaning within Xcode. It's the build phases under the targets that actually do the work of putting together a product.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With