在ViewControllers的iOS 7中使用JavaScriptCore从JavaScript触发目标C方法

我正在加载一个url,其中有HTML和JavaScript函数调用下面的web视图。 现在我正在查看用户在webview中触摸提交button时应该调用viewController中的任何方法。 WebView应该加载url(webview委托)是一种方法,但有什么新的方式来获得知道,当javascript函数被webview调用应通知viewcontroller或触发视图控制器中的任何方法。

我不在寻找这个我们需要的解决scheme

- (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 } 

我们必须从你的javascript方法导航到一个假的URL,

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

点击一个button或您需要的操作。

但JavaScriptCore有没有新的可能性来实现这一点。

iOS 7引入的JavascriptCore框架中的Objective-C接口允许从Javascript调用Objective-C方法。 要了解这些function的最佳介绍,请查看苹果开发人员networking2013年WWDC简介“将JavaScript集成到原生应用程序”会话: https : //developer.apple.com/videos/wwdc/2013/?id = 615

它在WebView上有一个简短的部分(MacOs不只是iOS)

下面的示例代码展示了如何实现你想要的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); } 

注意事项:

  • 访问UIWebView的JSContext没有logging。 两种技术是已知的(请参阅访问UIWebView的JavaScriptCore引擎 )。 第一种技术在这里使用。
  • 你不需要在页面中定义javascript函数“submitButton”,你将使用webView的JSContext定义Objective-C的函数
  • 在webview中加载页面会导致它的JSContext被replace,因此您应该为UIWebView实现一个委托,并在实现-webViewDidFinishLoad的select器中定义您的Objective-Ccallback:
  • 我假设你将要传递参数给这个callback,所以我已经显示了一个示例参数。 虽然上面提到的video教程(或者PDF格式)没有涉及到,但是查看JSValue.h显示JavascriptCore提供了以下Objective-C和Javascripttypes之间的内置转换:

  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.