search

Home  >  Q&A  >  body text

Objective-c - Packaged into .framework sdk. I want to put js files in it, but I don’t want to use bundle. How can I read the js files?

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?

过去多啦不再A梦过去多啦不再A梦2821 days ago836

reply all(2)I'll reply

  • 迷茫

    迷茫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.

    reply
    0
  • 给我你的怀抱

    给我你的怀抱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:

    1. File in Framework 的工程里面,Target->Build Phases->Copy Bundle Resources 里面加入你需要打包进 Framework

    2. 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!

    reply
    0
  • Cancelreply