小编典典
在JS中调用Objective-C的方法:
下面的网址有助于做到这一点
无法执行,我们通过导航到其他页面来解决,在导航期间,Webview委托将监视导航的前缀并执行我们指定的方法。
sample.html
function getText()
{
return document.getElementById('txtinput').value;
}
function locationChange()
{
window.location = 'ios:webToNativeCall';
}
Hi Welcome to Webpage
Please Click the button after entering the text
Objective-C代码
当您单击html页面中的按钮时,以下委托将被触发并取消导航,因为我们返回NO且调用了相应的方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[[request URL] absoluteString] hasPrefix:@"ios:"]) {
// Call the given selector
[self performSelector:@selector(webToNativeCall)];
// Cancel the location change
return NO;
}
return YES;
}
- (void)webToNativeCall
{
NSString *returnvalue = [self.webviewForHtml stringByEvaluatingJavaScriptFromString:@"getText()"];
self.valueFromBrowser.text = [NSString stringWithFormat:@"From browser : %@", returnvalue ];
}
2020-05-01