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;
开发者_Go百科}
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?
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.
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.
It is not that the Xcode dont't like .js files. It does consider them else we would have not used .js files for javascriptexecution in webview.
It is just that we need to add the .js file in Copy Bundle resource build phase under targets. Just drag your .js file into the copy bundle resource build phase. Thus Xcode will consider this file since it is now in the bulid phase bundle. I think we if we don't do the above step, we get a warning on building the code. I don't exactly remember the warning but it just said that Xcode cannot recognize .js file.
Once the file is added to the build phase, you can use NSData *jsData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"global" ofType:@"js"]];
I faced the same problem.
I performed the following steps to solve this problem :
Locate the js file into "compile source" section in the build settings and remove it from there.
Locate "copy Bundle Resource" section and add the js file there.
The above 2 steps solved my problem..
sometimes xCode doesn't realize a file is in your project, and doesn't bundle everything up correctly. try removing the .js file and adding it again.
also, it seems that you are mentioning 2 different file names, activity.js and global.js
精彩评论