0

In my application , i have to change font from webPage that loaded from online.

First i store my CustomJava.js files in online hosting and i use that file to change font in my iOS app.

Here is some of my codes.

- (void)webViewDidFinishLoad:(UIWebView *)webView
{  
    NSString *js = [NSString stringWithFormat:@"var script = document.createElement('script');"
                    "script.type = 'text/javascript';"
    "script.src = 'http://www.gustohei.com/SYjs/customJava.js';"];

    js = [NSString stringWithFormat:@"%@ document.getElementsByTagName('head')[0].appendChild(script);", js];
    [self.webViewOfBrowser stringByEvaluatingJavaScriptFromString:js];

    UIApplication *app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = NO;
}

It's fine and change font correctly.

In my case , i want to keep that JS File in my document directory and want to use that file from document directory . i don't want to use from online.

So i wrote following code.

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *jsPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"customJava.js"];

    NSString *js = [NSString stringWithFormat:@"var script = document.createElement('script');"
                    "script.type = 'text/javascript';"
                    "script.src ='%@'",jsPath];

    js = [NSString stringWithFormat:@"%@ document.getElementsByTagName('head')[0].appendChild(script);", js];
    [self.webViewOfBrowser stringByEvaluatingJavaScriptFromString:js];

    UIApplication *app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = NO;
}

However it's doesn't work. How to do that?

Fire Fist
  • 7,032
  • 12
  • 63
  • 109

2 Answers2

1
  1. In Xcode’s project navigator (left column) click on the blue project icon.
  2. Select the “Build Phases” tab in the main window
  3. Open the “Compile Sources” section
  4. Find your Javascript file in the list and remove it.
  5. Open the “Copy Bundle Resources” section
  6. Add your Javascript file to the list

Then load the js file using following code

[[NSBundle mainBundle] pathForResource:@"script.js" ofType:nil];

UPDATE: Xcode 4.5 now creates a warning when you just add a javascript file to your project without removing it from the “Compile Sources” section:

warning: no rule to process file 'FILEPATH' of type sourcecode.javascript for architecture armv7

iphonic
  • 12,615
  • 7
  • 60
  • 107
  • It's not work brother. I loaded same JS file into Bundle like you said.But font doesn't change like when i use JS file from Online. – Fire Fist Feb 14 '13 at 08:12
  • This link might help you then http://stackoverflow.com/questions/5733883/loading-javascript-into-a-uiwebview-from-resources – iphonic Feb 14 '13 at 08:13
0

Keep the javascript file and the HTML in same folder in documents directory and Just include the javascript in the HTML you are loading over webview

<script src = "CustomJava.js" type="application/javascript"></script>

and then call the javascript function in your code like this: (for example "changeFont();" is the name of the function you want to call over webview)

   NSString *javascriptFunctionCall = [NSString stringWithFormat:@"changeFont('%@');",self.fontSize];

   [bookWebView stringByEvaluatingJavaScriptFromString:javascriptFunctionCall];
Bhupendra
  • 2,525
  • 18
  • 20
  • i don't understand what you mean bro? i am loading page from web and want to insert js code into that loaded page from web in UIWebView. – Fire Fist Feb 14 '13 at 08:39
  • I am loading page like web browser and want to append my JS Code into that loaded page in UIWebView. – Fire Fist Feb 14 '13 at 08:41
  • okay you cant edit the page from web ? its not hosted on your server only ? – Bhupendra Feb 14 '13 at 08:41
  • No. I can append when i keep my JS File in hosting and read that js file and append into my loaded page, It's fine. I want to do with local file. not from hosting. – Fire Fist Feb 14 '13 at 08:42
  • ohhh !!! then i dont think giving local path to script.src will work as you did !! i need to think if its possible or not – Bhupendra Feb 14 '13 at 09:01
  • Yes . i don't know how to do that. :( – Fire Fist Feb 14 '13 at 09:06