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...

0

2 Answers 2

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!\")"];
Sign up to request clarification or add additional context in comments.

4 Comments

Is it possible to use jQuery instead? Also what if I have a whole bunch of js library/plugin to add?
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.
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...
Absolutely, if you set the UIWebView's delegate, you can receive these two calls called webViewDidStartLoad: and webViewDidFinishLoad:
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

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.