Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS CDVWKInAppBrowser evaluateJavascript method randomly gets blocked on iOS 12 #340

Closed
jonathanli2 opened this issue Nov 2, 2018 · 8 comments

Comments

@jonathanli2
Copy link
Contributor

jonathanli2 commented Nov 2, 2018

Hi,
While testing the new ios inappbrowser plugin with wkwebview on iOS 12 device, when the javascript code calls injectScriptCode method, the application may hang.
The reason is in CDVWKInAppBrowser.m, the completing block of evaluateJavascrip method is not called, so the method stuck in the while (!finished) loop

_- (NSString *)evaluateJavaScript:(NSString *)script {
__block NSString resultString = nil;
__block BOOL finished = NO;
__block NSString
_script = script;
NSLog(@"evaluateJavaScript: %@", script);
[self.inAppBrowserViewController.webView evaluateJavaScript:script completionHandler:^(id result, NSError *error) {
NSLog(@"evaluatejavascript result: %@, %@", result, error);
if (error == nil) {
if (result != nil) {
resultString = result;
NSLog(@"%@", resultString);
}
} else {
NSLog(@"evaluateJavaScript error : %@ : %@", error.localizedDescription, _script);
}
finished = YES;
}];

while (!finished)
{
    NSLog(@"CDVWKInAppbrowser evaluateJavaScript runloop");
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
NSLog(@"evaluateJavaScript: %@", resultString);
return resultString;

}_

The xcode console log has the below output. After calling evaluatejavascript method, it calls runloop method few times in while block, and then just stops, the app is idle.

2018-11-02 09:44:32.646767-0400 wk[5669:2032638] evaluateJavaScript: (function(w){if(!w._cdvMessageHandler) {w._cdvMessageHandler = function(id,d){w.webkit.messageHandlers.cordova_iab.postMessage({d:d, id:id});}}})(window)
2018-11-02 09:44:39.013119-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.013583-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.013827-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.014057-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.014336-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.014561-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.014789-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.015012-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.015234-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.015455-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.015676-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.015898-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.016120-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.016400-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.016622-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.016848-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.017239-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.021299-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.037837-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:44:39.054533-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop
2018-11-02 09:45:47.383791-0400 wk[5669:2032638] CDVWKInAppbrowser evaluateJavaScript runloop

I saw someone else also reported the same issue on ios 12 device testing at
https://stackoverflow.com/questions/26778955/wkwebview-evaluate-javascript-return-value
(see the first answer), so this may relate to the iOS 12 behavior change.

Thanks for help.

Jonathan

@jonathanli2
Copy link
Contributor Author

Add the comment from
https://stackoverflow.com/questions/26778955/wkwebview-evaluate-javascript-return-value

"This solution IS NOT WORKING anymore. The callback now called from the main Thread and if you lock the main thread in the while loop (like in this solution), the callback handler will be never called. evaluateJavaScript waits for main thread released, but it never happens because it locked in the loop. – Mike Keskinov Sep 4 at 0:21"

@jonathanli2
Copy link
Contributor Author

As the return value from CDVWKInAppbrowser's evaluateJavascript method is actually never used, it may well just changes the function signature to
- (void)evaluateJavaScript:(NSString *)script
and so we can remove the while loop which blocks the method from returning.

Thanks
Jonathan

@jonathanli2
Copy link
Contributor Author

A change has been made at the pull request
#341

@janpio
Copy link
Member

janpio commented Nov 6, 2018

Could you take a look please @dpa99c?

@dpa99c
Copy link
Contributor

dpa99c commented Nov 6, 2018

The PR changes look good to me:
as @jonathanli2 points out, the synchronous return value is never used in CDVWKInAppbrowser since the result is returned asynchronously via the postMessage API.
The loop was just a hangover from the UIWebView implementation that I forgot to take out.

@jonathanli2
Copy link
Contributor Author

Any update for this pull request?

janpio pushed a commit that referenced this issue Nov 19, 2018
…cked on ios 12 (#341)

### Platforms affected
iOS

### What does this PR do?
fix issue[ #340](#340) iOS CDVWKInAppBrowser evaluateJavascript method randomly gets blocked on iOS 12

### What testing has been done on this change?
manual testing

### Checklist
- [x] [Reported an issue](http://cordova.apache.org/contribute/issues.html) in the JIRA database
- [x] Commit message follows the format: "CB-3232: (android) Fix bug with resolving file paths", where CB-xxxx is the JIRA ID & "android" is the platform affected.
- [ ] Added automated test coverage as appropriate for this change.
@jonathanli2
Copy link
Contributor Author

The pull request has been merged, and the issue is closed.

Thanks
Jonathan

@janpio
Copy link
Member

janpio commented Nov 20, 2018

(FYI: If you use closes #340 in the text of a PR, it will automatically close the issue when the PR is merged. Can use that for your next PR.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants