pbserious
pbserious

Reputation: 13

Which method should i inject javascript? (stringByEvaluatingJavaScriptFromString)

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:

Thanks for reading and any help/suggestion. :)

Upvotes: 1

Views: 1678

Answers (2)

Sakares
Sakares

Reputation: 606

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 !

Upvotes: 0

Rob Napier
Rob Napier

Reputation: 299545

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.

Upvotes: 1

Related Questions