1

I have this iOS app written in objective-c with a UIWebview in it, the main story happens in the UIWebview. I don't want to load every page from server because there will be some lag, and I wonder if I could embed the interface/pages that are written in html/js locally in the native code? So the UIWebview of the app won't have to load the page from server remotely every time but locally? Basically this is still a native app so I can make use of push notification and the like seamlessly, didn't like Phonegap so...

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
dulan
  • 1,584
  • 6
  • 22
  • 50

2 Answers2

3

It is possible to execute javascript on a UIWebview, you can then use that javascript to insert whatever you want. This is the method:

[webview stringByEvaluatingJavaScriptFromString:@"alert(\"Your inserted javascript here!\")"];
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Patrick T Nelson
  • 1,234
  • 12
  • 21
  • Is it possible to use jQuery instead? Also what if I have a whole bunch of js library/plugin to add? – dulan Nov 28 '14 at 05:38
  • It is possible, yes, but you'd have to pass the entire JQuery script using the same method. It may be easier to write a function that takes a file, reads it, and inserts it in to the webview, that way you can just have the .js file in your bundle somewhere and load it more easily, instead of passing in a massive string in your source. – Patrick T Nelson Nov 28 '14 at 05:41
  • I see your point. Thanks for the tip! Is it possible to forbid the webview showing anything until the page is fully loaded? That way I can just put an activityIndicator on top of it, would make my life much easier... – dulan Nov 28 '14 at 05:50
  • Absolutely, if you set the UIWebView's delegate, you can receive these two calls called webViewDidStartLoad: and webViewDidFinishLoad: – Patrick T Nelson Nov 28 '14 at 05:57
0

In your UIWebView delegate, provide an implementation for

webView:shouldStartLoadWithRequest:navigationType:

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest

navigationType:(UIWebViewNavigationType)inType

{

if ( [[[inRequest URL] scheme] isEqualToString:@"callback"] ) {

    // Do something interesting...

    return NO;
}

return YES;

}

here :Javascript in UIWebView callback to C/Objective-C

Community
  • 1
  • 1
Shanmugasundharam
  • 2,082
  • 23
  • 32