Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a method by clicking a button in a UIWebView

I want to generate some HTML content and put this into a UIWebView. The HTML contains some buttons. Is it possible to define actions for these buttons? I want to call a method (e.g. firstButtonPressed) in my objective-c code when someone presses this button in the UIWebView.

Thanks you for helping me.

like image 914
Dominik Avatar asked Mar 02 '10 13:03

Dominik


1 Answers

Just to follow up on this, it is possible, and not to difficult to connect HTML/javascript events to objective-c methods.

Damien Berrigaud came up with a good example of how to do that by using file:// links

// Map links starting with file://
//            ending with #action
// with the action of the controller if it exists.
//
// Open other links in Safari.
- (BOOL)webView: (UIWebView*)webView shouldStartLoadWithRequest: (NSURLRequest*)request navigationType: (UIWebViewNavigationType)navigationType {
  NSString *fragment, *scheme;

  if (navigationType == UIWebViewNavigationTypeLinkClicked) {
    [webView stopLoading];
    fragment = [[request URL] fragment];
    scheme = [[request URL] scheme];

    if ([scheme isEqualToString: @"file"] && [self respondsToSelector: NSSelectorFromString(fragment)]) {
      [self performSelector: NSSelectorFromString(fragment)];
      return NO;
    }

    [[UIApplication sharedApplication] openURL: [request URL]];
  }

  return YES;
}
like image 58
Jehiah Avatar answered Nov 15 '22 06:11

Jehiah