iOS调用js的方法 alert不能点击的原因

The gist is that the thread handling the JS from the stringByEvaluatingJavaScriptFromString: method and the thread handling the iOS alert view are probably blocking each other, making the “Close” button unresponsive.
it might be better to have the UIWebView trigger an UIAlertView rather than rely on UIWebView to handle the JS alert. The workaround above would be suitable for most debugging purposes though.
关键点在于,使用StrugByEngutyActudiaJavaScript执行JS方法的线程和执行iOS弹框视图的线程可能彼此阻塞,使得“关闭”按钮无响应。
最好让UIWebVIEW触发一个alert视图,而不是依赖于UIWebView来处理JS的alert。不过下面这种方式适用于大多数的调试了
NSString *functionStr = [NSStringstringWithFormat:@”postAction(%zd)”,action];
NSString *funcScript =[NSString stringWithFormat:@”setTimeout(function(){%@;},1)”,functionStr];
[_context evaluateScript:funcScript];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在JavaScript中调用iOS方法需要使用JavaScript与原生iOS之间的交互技术。以下是一些方法: 1. 使用UIWebView执行JavaScript代码 可以使用UIWebView在iOS应用程序中执行JavaScript代码。UIWebView对象提供了一个方法,可以通过该方法将JavaScript代码传递给UIWebView并执行它。在JavaScript代码中,可以使用window.location来调用iOS方法。例如: ``` window.location = "ios://methodName?param1=value1&param2=value2"; ``` 在iOS应用程序中,可以在UIWebView的委托方法中截取这个请求,解析参数,并调用相应的方法。例如: ``` - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSURL *url = request.URL; if ([[url scheme] isEqualToString:@"ios"]) { NSString *methodName = [url host]; NSDictionary *params = [self parseParams:[url query]]; // call the iOS method with the given name and parameters [self callMethod:methodName withParams:params]; return NO; } return YES; } ``` 2. 使用JavaScriptCore框架 JavaScriptCore框架是一个内置于iOS中的JavaScript引擎。可以使用它来执行JavaScript代码并与原生iOS进行交互。可以使用JSContext对象来将JavaScript代码传递给JavaScript引擎并执行它。在JavaScript代码中,可以使用JSContext对象的evaluateScript方法调用iOS方法。例如: ``` var methodName = "methodName"; var param1 = "value1"; var param2 = "value2"; var result = MyObjCClass.callNativeMethod(methodName, param1, param2); ``` 在iOS应用程序中,可以创建一个MyObjCClass类来处理JavaScript传递的调用请求,并返回结果。例如: ``` JSContext *context = [[JSContext alloc] init]; context[@"MyObjCClass"] = [MyObjCClass class]; [context evaluateScript:@"var methodName = 'methodName'; var param1 = 'value1'; var param2 = 'value2'; var result = MyObjCClass.callNativeMethod(methodName, param1, param2);"]; ``` 以上是两种在JavaScript中调用iOS方法方法。当然,具体的实现方案需要根据具体的需求和场景进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值