gpt4 book ai didi

ios - objective-c 将关键事件发送到 webview

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:08:53 26 4
gpt4 key购买 nike

只是想知道是否有办法将按键事件或文本发送到 WebView 。

我有一个应用程序,用户可以点击一个按钮来执行特定的任务。根据用户单击的按钮,需要填充 Web View 内的文本框。

我可以处理按钮点击事件等等,但只是想看看我是否可以将该文本传递给 webview。

我在谷歌上搜索过,但还没有找到任何解决方案。也许我遗漏了什么。

无论如何,先谢谢了。

最佳答案

HTML 端:

假设以下是在您的 Objective-C 方法中调用它时触发的 javascript 方法..即 native 端。

<script type="text/javascript">

var htmlTouch = 0;

//following is the function that you've assigned for a HTML button.

function button1_click()
{
htmlTouch =1; //The reason why i am setting the htmlTpuch=1,is just to identify whether it's native touch or HTML touch.
}

//javascrip function which we are going to call from oObjective-c
function toCallFromiOS()
{

if( htmlTouch == 1 )
{
alert("its a html touch,so pass any values to native code here.");
return 'urText';
}

}

//To reset the touch for next use.
function resetTheHTMLtouch()
{
htmlTouch = 0;
}

</script>

native 端:

创建 UIWebview 来加载上面的 html(好吧,我现在在本地做)。

self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"test" ofType:@"html"] isDirectory:NO]]];

现在将手势委托(delegate)添加到整个 webview。

UITapGestureRecognizer *tapGestureDown = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture)];
tapGestureDown.numberOfTapsRequired = 1;
tapGestureDown.delegate = self;
[self.webView addGestureRecognizer:tapGestureDown];

//handleTapGesture 是一种 native 方法,在“检测它是否是 native 触摸时,你想要执行什么?”的意义上。

-(void)handleTapGesture
{
NSLog(@"Touch is native");
}

现在我们都准备好了。下一步是实现委托(delegate)

-shouldRecognizeSimultaneouslyWithGestureRecognizer:==>which returns BOOL value.

在检测到 webview 上的触摸事件时,调用已实现的委托(delegate)函数。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}

如果你像那样添加上面的代码,在点击 webview 时,上面的委托(delegate)被调用 N 次(有时是 8、9、13 等)。唯一的解决方案是我们应该能够知道状态触摸(无论是结束还是开始),为下一次调用重置触摸事件。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
NSString *javastr=[self.webView stringByEvaluatingJavaScriptFromString:@"toCallFromiOS();"];
NSLog(@"This is return string from javascript==>%@",javastr);

if((otherGestureRecognizer.state==UIGestureRecognizerStateEnded && [javastr hasPrefix:@"urText"]))
{

javastr= [self.webView stringByEvaluatingJavaScriptFromString:@"resetTheHTMLtouch();"];

return NO;
}

return YES;

}

如果 javastr 返回任何值(文本),则它是 HTML 触摸或者它的 native 触摸,"handleTapGesture"被调用。

有关更多详细信息,请查看我的博客==> Feel the difference between HTML touch and Native touch on UIWebView

希望这对您有帮助。祝您编码愉快...

关于ios - objective-c 将关键事件发送到 webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18731474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com