最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get a js file path in iOS? - Stack Overflow

programmeradmin0浏览0评论

I dragged and dropped a .js file in to my project. I want to load it, but it's not working. It gives me a null path .

This is the first I try to get the path

NSString *path = [[NSBundle mainBundle] pathForResource:@"SearchWebView" ofType:@"js"];

this is not working. So then I think I might need to copy it in Documents/

So I run the copy file

- (NSString *)getJSFilePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"SearchWebView.js"];
}

- (void) copyJSFileIfNeeded{
    //Using NSFileManager we can perform many file system operations.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *jsPath = [self getJSFilePath];
    BOOL success = [fileManager fileExistsAtPath:jsPath]; 

    if(!success) {
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"SearchWebView.js"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:jsPath error:&error];
        if (!success){ 
            //NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
        }
    }
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    [self performSelector:@selector(copyJSFileIfNeeded)];

}

It's not creating a new copy file in my documents folder. I don't know why? I can use the same method to copy plist or sqlite file, But the ".js file" is no response . I also check the file is exactly in my project bundle .

I dragged and dropped a .js file in to my project. I want to load it, but it's not working. It gives me a null path .

This is the first I try to get the path

NSString *path = [[NSBundle mainBundle] pathForResource:@"SearchWebView" ofType:@"js"];

this is not working. So then I think I might need to copy it in Documents/

So I run the copy file

- (NSString *)getJSFilePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"SearchWebView.js"];
}

- (void) copyJSFileIfNeeded{
    //Using NSFileManager we can perform many file system operations.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *jsPath = [self getJSFilePath];
    BOOL success = [fileManager fileExistsAtPath:jsPath]; 

    if(!success) {
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"SearchWebView.js"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:jsPath error:&error];
        if (!success){ 
            //NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
        }
    }
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    [self performSelector:@selector(copyJSFileIfNeeded)];

}

It's not creating a new copy file in my documents folder. I don't know why? I can use the same method to copy plist or sqlite file, But the ".js file" is no response . I also check the file is exactly in my project bundle .

Share Improve this question edited Dec 12, 2012 at 9:03 Srikar Appalaraju 73.7k55 gold badges219 silver badges265 bronze badges asked Mar 5, 2012 at 10:48 Webber LaiWebber Lai 2,0226 gold badges35 silver badges69 bronze badges 3
  • Delete "SearchWebView.js" in your project and again drag your file into your project and check option to "copy that file into destination folder". – Dinesh Raja Commented Mar 5, 2012 at 10:57
  • Why trying to copy the file if !success ?? – user971401 Commented Mar 5, 2012 at 10:57
  • @Vince because I don't know why the file I can't get in main bundle ... – Webber Lai Commented Mar 6, 2012 at 1:27
Add a ment  | 

1 Answer 1

Reset to default 13

When you add the JavaScript file, Xcode detects that the file is a source code file, assumes you want to pile it and automatically adds it to the Compile Sources build phase.

To stop Xcode trying to pile it and make it copy the file instead, expand your target in the Groups and Files list, remove the JavaScript file from the Compile Sources build phase and add it to the Copy Bundle Resources build phase.

For Xcode 4, click on the main project, click on Build Phases, and you'll find Compile Sources and Copy Bundle Resources, detailed steps below -

  1. Project Navigator (Cmd + 1)
  2. Select your project
  3. Select target
  4. Select "Build Phases" tab, there you will get your desire sections.
  5. Add desired js file to copy bundle resources and remove from pile sources
发布评论

评论列表(0)

  1. 暂无评论