简体   繁体   中英

Catching JavaScript asynchronous calls with Objective-c

I catch javascript calls with UIWebViewDelegate .

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. So if I get two request same time the webview delegate can see only one of them and ignore the other one.

I tried to use NSOperationQueue and NSURLConnection sendAsynchronousRequest but without any success.

How could I invoke shouldStartLoadWithRequest delegate for each async JS request?

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.

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

I know it's complicated, but it's a solution... Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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