gpt4 book ai didi

javascript - 判断何时 stringByEvaluatingJavaScriptFromString : has completed

转载 作者:行者123 更新时间:2023-11-28 07:20:03 24 4
gpt4 key购买 nike

我正在使用标准 UIWebView,并且需要知道 stringByEvaluatingJavaScriptFromString: 何时完成。我知道 WKWebView 有一个完成处理程序,但我需要使用给我的当前 UIWebView 。由于该方法需要在主线程上运行,GCD 没有多大帮助。有什么建议么?

-(void)insertValue:(NSString*)value inWebView:(UIWebView*)webView{
NSString *jsString = [NSString stringWithFormat:kConstantJSString, value];
[webView stringByEvaluatingJavaScriptFromString: jsString];
NSLog("THIS SHOULD LOG WHEN JS HAS EXECUTED");
}

最佳答案

好的,所以我想出了一个好方法来做到这一点。在您的 javascript 调用中添加以下行到脚本末尾:

window.location = 'js-ObjC:jsCompleted';

然后使用 UIWebView 的 shouldStartLoadWithRequest: 方法来捕获并测试这个新窗口位置。然后,我使用新窗口位置中第二个组件的名称调用了一个选择器。

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
if ([[[request URL] absoluteString] hasPrefix:@"js-ObjC:"]) { //get location change
NSArray *components = [[[request URL]absoluteString] componentsSeparatedByString:@":"];
NSString *functionName = [components objectAtIndex:1];

/* Calls selector for functionName, this way silences compiler warning */
SEL selector = NSSelectorFromString(functionName);
((void (*)(id, SEL))[self methodForSelector:selector])(self, selector);

return NO;
}
return YES;

}

-(void)jsCompleted{
//Do stuff after javascript has completed

}

关于javascript - 判断何时 stringByEvaluatingJavaScriptFromString : has completed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30400469/

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