user1028880
user1028880

Reputation:

Invoke Native code (objectiveC) from UIWebView HTML (JavaScript)

I've read

Invoke method in objective c code from HTML code using UIWebView

They do

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

Here, a problem of my code is that I do not have

UIWebView's delegate's shouldStartLoadWithRequest method, since I simply implement UIWebView directly in AppDelegate like below:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{       
    UIWebView *view = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    NSString* path = [[NSBundle mainBundle] pathForResource:@"main" ofType:@"html" inDirectory:@"www"];
    [view loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
    view.scalesPageToFit = NO;
    UIViewController *controller = [[UIViewController alloc] init];
    controller.view = view;

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = controller;
    [self.window makeKeyAndVisible];

    return YES;    
}

I like this way because of the simpler structure of code and files.

So my question is, is it possible to implement call-native-from-webView in this style?

or, do I need to have an independent UIWebView Controller/Delegate?

If possible, can you show me the example how to do that?

I have not done much for objectiveC codes and MVC models, so appreciated if you show me the way. Thanks.

Upvotes: 0

Views: 441

Answers (1)

nhgrif
nhgrif

Reputation: 62072

The webView:shouldStartLoadWithRequest:navigationType: method will be called on whatever object is set as the webview's delegate.

Upvotes: 1

Related Questions