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?

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
This link might help you then stackoverflow.com/questions/5733883/…
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];

6 Comments

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.
I am loading page like web browser and want to append my JS Code into that loaded page in UIWebView.
okay you cant edit the page from web ? its not hosted on your server only ?
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.
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.