Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find .js file in my mainBundle?

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?

like image 850
Nic Hubbard Avatar asked Dec 27 '10 20:12

Nic Hubbard


People also ask

Where are .JS files stored?

Javascript files are stored on the server. They're sent to the browser the same way HTML, CSS and image files are sent.

How do I open .JS files?

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.

What are .JS files on my computer?

A JS file is a plain text file that contains JavaScript code. It is used to execute JavaScript instructions in a webpage.

Why JS file is not working in HTML?

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.


Video Answer


1 Answers

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.

like image 170
Chris Hanson Avatar answered Oct 05 '22 03:10

Chris Hanson