Calling [JSValue callWithArguments:] locks UI when alert() is called

前端 未结 1 1759
春和景丽
春和景丽 2021-02-06 08:53

Using the JSContext from a UIWebView I have created a javascript function that is implemented as an Objective C block:

JSContext *js =          


        
1条回答
  •  天涯浪人
    2021-02-06 09:28

    After a couple days looking at stack traces of threads waiting for each other, the solution was so simple I'm not surprised I overlooked it in favor of trying more complicated stuff.

    If you want to call back into a UIWebView's javascript asynchronously, use window.setTimeout and let the JSVirtualMachine take care of queuing the callback.

    Just replace

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.callback callWithArguments:@[arg]];
    });
    

    with

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.callback.context[@"setTimeout"] callWithArguments:@[self.callback, @0, arg]];
    });
    

    0 讨论(0)
提交回复
热议问题