在ViewController中使用iOS 7中的JavaScriptCore从javascript触发目标C方法
我正在webview中加载一个url,它有下面的HTML和javascript函数调用。现在我正在研究当用户在webview中点击submit按钮时,它应该调用viewController中的任何方法。Webview应该加载url(Webview委托)是一种方法,但是否有任何新的方法可以知道Webview何时调用javascript函数。Webview应该通知viewcontroller或触发viewcontroller中的任何方法 我不是在寻找我们需要的解决方案在ViewController中使用iOS 7中的JavaScriptCore从javascript触发目标C方法,javascript,ios,objective-c,ios7,javascriptcore,Javascript,Ios,Objective C,Ios7,Javascriptcore,我正在webview中加载一个url,它有下面的HTML和javascript函数调用。现在我正在研究当用户在webview中点击submit按钮时,它应该调用viewController中的任何方法。Webview应该加载url(Webview委托)是一种方法,但是否有任何新的方法可以知道Webview何时调用javascript函数。Webview应该通知viewcontroller或触发viewcontroller中的任何方法 我不是在寻找我们需要的解决方案 - (BOOL)webVie
- (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";
单击按钮或执行所需操作
但是JavaScriptCore是否有新的可能性实现同样的功能。iOS 7引入的JavaScriptCore框架中的Objective-C接口允许从Javascript调用Objective-C方法。有关这些功能的精彩介绍,请查看2013年WWDC简介“将JavaScript集成到本机应用程序”课程,该课程位于苹果开发者网络: 它的结尾有一个关于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);
}
这是我正在加载我的移动应用程序的示例HTML页面请输入详细信息您可以创建一个phonegap应用程序,您可以在webview处调用该应用程序。将执行与您希望的相同的操作Hanks Rahul,但苹果最近在iOS 7中引入的JavaScriptCore框架不可能。Thank Dark的可能副本,但该链接具有在web视图中调用假url的相同方法。JS上下文的工作方式类似于[webView StringByEvaluating JavaScriptFromString:@“document.getElementById('lastname').value;”];但它提供的不仅仅是这些。感谢Mike的解释。@TMacGyver-苹果公司还没有正式记录他们在iOS7发布时添加到JavaScriptCore框架中的Objective-C扩展,因此这两种已知技术中的任何一种都可能会带来不同的效果。我还没有提交使用这两种技术的应用程序,但是已经有了报告使用第一种技术(如上面的示例中所使用的)成功提交的ts。请参阅mprudhom对
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.