Search code examples
iphonecocoa-touchnsdatansbundle

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?


Solution

  • As far I remembered Xcode doesn't like files with *.js extension. I mean it didn't add it into application bundle. Just rename your file to *.txt and all will be ok.

    And right answer will be: Your need to move your js files from Compile Sources target build phase to Copy Bundle Resources. Because Xcode thinks that js files are source files and they should be compiled instead then added as resources.