1

I have create an IOS app.

The app has MainViewController that will present/dismiss Modal view from webViewController nib file(contains NavBar and UIWebView). I want to inject my javascript functions in every web pages that loaded by the UIWebView and those functions will be call in webpages that means all functions had to finish load before pages finish load.

I try many ways but it seem like when the ModalView present again(it's work fine at first present).The page doesn't know my injected function.

So my question is:

  • Where should i inject my javascript if the UIWebView load request when the ModalView present? Before loadRequest? didStartLoad? didFinishLoad?
  • What should i do with UIWebView when dismiss the ModalView? load the about:blank request?

Thanks for reading and any help/suggestion. :)

2 Answers 2

1

Where should i inject my javascript if the UIWebView load request when the ModalView present? Before loadRequest? didStartLoad? didFinishLoad?

In webViewDidFinishLoad:. You cannot call stringByEvaluatingJavaScriptFromString: until this point. There is unfortunately no good way to get to this point without displaying the web view, which can lead sometimes to the view "flashing" if your JavaScript changes things too much. I sometimes wind up putting an extra view over the web view to hide it until after webViewDidFinishLoad: completes.

What should i do with UIWebView when dismiss the ModalView? load the about:blank request?

Nothing. It should be released when the modal view is released.

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

2 Comments

Thanks Rob :), I'll try it.I just worried about webpages will call those functions before it has injected and it'll make js crash.
You cannot fully fix this problem using stringByEvaluatingJavaScriptFromString:. The onload event will fire before you can run anything. If you have to inject code before onload, then you have to modify the HTML itself on the way down. You can do that with an NSURLProtocol, but modifying arbitrary HTML is always dangerous.
0

Where should i inject my javascript if the UIWebView load request when the ModalView present? Before loadRequest? didStartLoad? didFinishLoad?

I guess your situation, if user click some your target link or html element then the mainViewController will be appear with presentModalView Method ,right? (If it's not please correct me)
As situation above, you can call javascript in UIWebviewDelegate like this

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType 
{
     NSString *url = [[inRequest URL] absoluteString];

     /* separate your href string such as href="doMyFunction?presentView" */
     NSArray *comp1 = [url componentsSeparatedByString:@"?"];
     NSString * checker = [comp1 objectAtIndex:1];
     if([checker isEqualToString:@"presentView"])
     {
         // do my code
     }
}

What should i do with UIWebView when dismiss the ModalView? load the about:blank request?

Your MainView already cover up your UIWebview so it depends on your purpose like

  • change some information
  • hide some component
  • do not anything

Hope it helps you !

Comments

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.