C object trigger method from javascript using JavaScriptCore in iOS 7 in ViewControllers

I am loading a url into a webview which has below HTML and javascript function call. Now I study, when the user touches the submit button in webview, he should call any method in viewController. Webview should load the url (web view delegate) - this is one approach, but is there any new way to find out when the javascript function is called by the web browser, should the view dispatcher inform or run any method in the view controller.

I am not looking for this solution that we need

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ NSLog(@"passed data from web view : %@",[[request URL] query]); if([[[request URL] query] isEqualToString:@"clickedOnLineNo"]) { //Do your action here } 

We need to go to the fake url from your javascript method, e.g.

 window.location = "someLink://yourApp/form_Submitted:param1:param2:param3"; 

by pressing a button or the desired action.

But are there any new JavaScriptCore features to achieve the same.

+8
javascript ios objective-c ios7 javascriptcore
source share
1 answer

The JavascriptCore Objective-C interface introduced in iOS 7 allows you to call Objective-C methods from Javascript. To familiarize yourself with these features, check out the introduction to JavaScript integration in native applications in 2013 on the Apple developerWorks network: https://developer.apple.com/videos/wwdc/2013/?id=615

It has a short section towards the end in WebView (MacOS not iOS only)

The sample code below shows how to implement what you want for iOS.

 - (void)viewDidLoad { [super viewDidLoad]; UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,40,320,320)]; webView.delegate = self; [self.view addSubview:webView]; NSString *pageSource = @"<!DOCTYPE html> <html> <head> </head> <body> <h1>My Mobile App</h1> <p>Please enter the Details</p> <form name=\"feedback\" method=\"post\" action=\"mailto:you@site.com\"> <!-- Form elements will go in here --> </form> <form name=\"inputform\"> <input type=\"button\" onClick=\"submitButton('My Test Parameter')\" value=\"submit\"> </form> </body> </html>"; [webView loadHTMLString:pageSource baseURL:nil]; } - (void)webViewDidFinishLoad:(UIWebView *)webView { JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; // Undocumented access context[@"submitButton"] = ^(NSString *param1) { [self yourObjectiveCMethod:param1]; }; } - (void)yourObjectiveCMethod:(NSString *)param1 { NSLog(@"User clicked submit. param1=%@", param1); } 

Notes:

  • Access to JSContext UIWebView is undocumented. Two methods are known (see Accessing the JavaScriptCore UIWebView Engine ). The first method is used here.
  • You do not need to define a javascript function "submitButton" inside the page, you define a function from Objective-C using webView JSContext
  • Loading a page into a webview leads to replacing it with JSContext, so you must implement the delegate for UIWebView and define your Objective-C callback in your selector implementation for -webViewDidFinishLoad:
  • I suggested that you want to pass parameters to this callback, so I showed an example parameter. Although this is not covered in the above video tutorial (or PDF equivalent), looking at JSValue.h shows that JavascriptCore offers a built-in conversion between the following Objective-C and Javascript types:

.

  Objective-C type | JavaScript type --------------------+--------------------- nil | undefined NSNull | null NSString | string NSNumber | number, boolean NSDictionary | Object object NSArray | Array object NSDate | Date object NSBlock * | Function object * id ** | Wrapper object ** Class *** | Constructor object *** * Instances of NSBlock with supported arguments types will be presented to JavaScript as a callable Function object. For more information on supported argument types see JSExport.h. If a JavaScript Function originating from an Objective-C block is converted back to an Objective-C object the block will be returned. All other JavaScript functions will be converted in the same manner as a JavaScript object of type Object. ** For Objective-C instances that do not derive from the set of types listed above, a wrapper object to provide a retaining handle to the Objective-C instance from JavaScript. For more information on these wrapper objects, see JSExport.h. When a JavaScript wrapper object is converted back to Objective-C the Objective-C instance being retained by the wrapper is returned. *** For Objective-C Class objects a constructor object containing exported class methods will be returned. See JSExport.h for more information on constructor objects. 
+21
source share

All Articles