简体   繁体   English

使用Objective-c捕获JavaScript异步调用

[英]Catching JavaScript asynchronous calls with Objective-c

I catch javascript calls with UIWebViewDelegate . 我使用UIWebViewDelegate捕获了javascript调用。

It looks like: 看起来像:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{      
NSDictionary *dictionary = [[[JSBridgeController alloc] init] js:request];
if (dictionary) {
    if ([dictionary[@"methodName"] isEqualToString:@"gimmeUser"]) {
        [self.delegate gimmeUserJS: dictionary];
    }
    if ([ dictionary[@"methodName"] isEqualToString:@"initMe"]) {
        [self.delegate initMeJS: dictionary];
    }
}
return YES;}

The problem is on the web side for me. 对我来说,问题出在网络方面。 It has dozens such JS requests as in above code and send them asynchronous. 它具有上述代码中的数十个此类JS请求,并将它们异步发送。 So if I get two request same time the webview delegate can see only one of them and ignore the other one. 因此,如果我同时收到两个请求,则webview委托只能看到其中一个,而忽略另一个。

I tried to use NSOperationQueue and NSURLConnection sendAsynchronousRequest but without any success. 我尝试使用NSOperationQueueNSURLConnection sendAsynchronousRequest但没有成功。

How could I invoke shouldStartLoadWithRequest delegate for each async JS request? 如何为每个异步JS请求调用shouldStartLoadWithRequest委托?

Thanks for any help.. 谢谢你的帮助..

One way of going about this would be to generate a unique identifier for your view. 一种解决方法是为视图生成唯一的标识符。
Then, store it in a dictionary with references to your views. 然后,将其存储在引用了您的视图的字典中。 Your views can then pass the identifier back by navigating to "callback:callback?id="+your_passed_id in your JS. 然后,您的视图可以导航到JS中的“ callback:callback?id =“ + your_passed_id,从而将标识符传回。

if ( [[[inRequest URL] scheme] isEqualToString:@"callback"] ) {
    //then use the query string and parse for id 
    //in order to have individual control over callbacks
    NSString *queryString = [[inRequest URL] query];
}

for mor info on the callback pattern see: Javascript in UIWebView callback to C/Objective-C 有关回调模式的更多信息,请参见: UIWebView回调C / Objective-C中的Javascript

I know it's complicated, but it's a solution... Good luck! 我知道这很复杂,但这是一个解决方案……祝您好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM