Used NSString * jsStr = [[NSString alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"xx" ofType:@"js"], and kept reporting Error Domain=NSCocoaErrorDomain Code=258 "The file name is invalid , does anyone know how to solve it?
迷茫2017-05-02 09:35:31
Static libraries don’t seem to work, you must create bundles, and the js files in the bundle will be automatically converted into .txt files by the system. A better solution is to compress the js code and then perform URLEncode encoding to solve this problem.
给我你的怀抱2017-05-02 09:35:31
You can package the files you need together into Framework
里面,然后通过 NSBundle
去获取这个文件。不同的是不能使用 mainBundle
,因为此时的 mainBundle
指的引用 Framework
this program. Of course your JS file cannot be found.
Solution:
File in Framework
的工程里面,Target->Build Phases->Copy Bundle Resources
里面加入你需要打包进 Framework
Use when you need to read a file
NSBundle *bundle = [NSBundle bundleWithIdentifier:@"your.framework.id"];
if (bundle) {
NSString *filePath = [bundle pathForResource:@"somefile" ofType:@"js"];
// ...
}
PS: Framework
It needs to be a dynamic library!