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] Application crashes when processing onCreateWindow #579

Closed
al-tush opened this issue Nov 5, 2020 · 10 comments
Closed

[iOS] Application crashes when processing onCreateWindow #579

al-tush opened this issue Nov 5, 2020 · 10 comments
Labels
bug Something isn't working

Comments

@al-tush
Copy link

al-tush commented Nov 5, 2020

Environment

Technology Version
Flutter version 1.22.3
Plugin version 5 (lastest master, the same issue in 4.0.0)
Android version
iOS version 4.0.1 and 4.1 (other OS versions not tested)
Xcode version 12.1

Device information:
iPhone 8

Description

Crash application with force unwrap nil exception when processing onCreateWindow

@al-tush al-tush added the bug Something isn't working label Nov 5, 2020
al-tush pushed a commit to al-tush/flutter_inappwebview that referenced this issue Nov 5, 2020
@mateusftt
Copy link

Same issue here, any solution?

@dodatw
Copy link

dodatw commented Dec 29, 2020

I have same issue.

If I implement async and return value, it crash.

onCreateWindow: (controller, onCreateWindowRequest) async {
    print("onCreateWindow called with URL ${onCreateWindowRequest.url}");
    return true;
},

if I no return, no crash.

onCreateWindow: (controller, onCreateWindowRequest) {
    print("onCreateWindow called with URL ${onCreateWindowRequest.url}");
},

@Alexays
Copy link

Alexays commented Mar 9, 2021

Any news on this issue @pichillilorenzo?
(Sorry for the ping)
What about al-tush@8d28b5b fix?

@pichillilorenzo
Copy link
Owner

pichillilorenzo commented Mar 9, 2021

@Alexays I'm not able to reproduce the error.

This is my simple test code using the latest version of the plugin:

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();

  if (Platform.isAndroid) {
    await AndroidInAppWebViewController.setWebContentsDebuggingEnabled(true);
  }

  runApp(MaterialApp(
      home: MyApp()
  ));
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {

  InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
    crossPlatform: InAppWebViewOptions(
        javaScriptCanOpenWindowsAutomatically: true
    ),
    android: AndroidInAppWebViewOptions(
      supportMultipleWindows: true,
      useHybridComposition: true,
    ),);

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
          appBar: AppBar(title: Text("On Create Window test")),
          body: SafeArea(
              child: Column(children: <Widget>[
                Expanded(
                  child: InAppWebView(
                    initialData: InAppWebViewInitialData(
                        data: """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Flutter InAppWebView</title>
</head>
<body>
  <a style="margin: 50px; background: #333; color: #fff; font-weight: bold; font-size: 20px; padding: 15px; display: block;"
    href="https://flutter.dev"
    target="_blank">
    Click here to open https://github.com/flutter in a popup!
  </a>
</body>
</html>
"""
                    ),
                    initialOptions: options,
                    onCreateWindow: (controller, createWindowRequest) async {
                      print("Window ID is: " + createWindowRequest.windowId.toString());
                      print(createWindowRequest);
                      return true;
                    },
                  ),
                ),
              ])),
    );
  }
}

I simply print the request from onCreateWindow event and return true, but I don't see any crash.
It just works and doesn't crash.

Can you provide me some code that make it crash?

@Alexays
Copy link

Alexays commented Mar 9, 2021

@pichillilorenzo In my case it doesn't crash any more on the first click with the latest version but, reclicking on the same link after closing the new window crash the application.
I'll try to get a crash log

@Alexays
Copy link

Alexays commented Mar 9, 2021

2021-03-09 18:27:29.256377+0100 ---[65608:539117] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Completion handler passed to -[flutter_inappwebview.InAppWebView webView:didReceiveAuthenticationChallenge:completionHandler:] was not called'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff20421af6 __exceptionPreprocess + 242
	1   libobjc.A.dylib                     0x00007fff20177e78 objc_exception_throw + 48
	2   CoreFoundation                      0x00007fff204219d4 -[NSException initWithCoder:] + 0
	3   WebKit                              0x00007fff287533f0 _ZN6WebKit28CompletionHandlerCallCheckerD2Ev + 124
	4   WebKit                              0x00007fff2887f5e3 _ZN3WTF3RefIN6WebKit28CompletionHandlerCallCheckerENS_13DumbPtrTraitsIS2_EEED2Ev + 41
	5   WebKit                              0x00007fff288d7072 _ZZN3WTF8BlockPtrIFv36NSURLSessionAuthChallengeDispositionP15NSURLCredentialEE12fromCallableIZN6WebKit15NavigationState16NavigationClient33didReceiveAuthenticationChallengeERNS7_12WebPageProxyERNS7_28AuthenticationChallengeProxyEE3$_5EES5_T_ENUlPKvE_8__invokeESH_ + 18
	6   libsystem_blocks.dylib              0x00007fff2007f42f _Block_release + 128
	7   WebKit                              0x00007fff288d4928 _ZN6WebKit15NavigationState16NavigationClient33didReceiveAuthenticationChallengeERNS_12WebPageProxyERNS_28AuthenticationChallengeProxyE + 334
	8   WebKit                              0x00007fff28a57e5f _ZN6WebKit19NetworkProcessProxy33didReceiveAuthenticationChallengeEN3PAL9SessionIDEN3WTF16ObjectIdentifierINS_26WebPageProxyIdentifierTypeEEERKNS3_8OptionalIN7WebCore18SecurityOriginDataEEEONS8_23AuthenticationChallengeEby + 375
	9   WebKit                              0x00007fff286a8ab5 _ZN6WebKit19NetworkProcessProxy36didReceiveNetworkProcessProxyMessageERN3IPC10ConnectionERNS1_7DecoderE + 40357
	10  WebKit                              0x00007fff28649c2a _ZN3IPC10Connection15dispatchMessageENSt3__110unique_ptrINS_7DecoderENS1_14default_deleteIS3_EEEE + 696
	11  WebKit                              0x00007fff2864c644 _ZN3WTF6Detail15CallableWrapperIZN3IPC10Connection22enqueueIncomingMessageENSt3__110unique_ptrINS2_7DecoderENS4_14default_deleteIS6_EEEEE3$_7vJEE4callEv + 214
	12  JavaScriptCore                      0x00007fff325a196d _ZN3WTF7RunLoop11performWorkEv + 573
	13  JavaScriptCore                      0x00007fff325a247a _ZN3WTF7RunLoop11performWorkEPv + 26
	14  CoreFoundation                      0x00007fff2039038a __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
	15  CoreFoundation                      0x00007fff20390282 __CFRunLoopDoSource0 + 180
	16  CoreFoundation                      0x00007fff2038f764 __CFRunLoopDoSources0 + 248
	17  CoreFoundation                      0x00007fff20389f2f __CFRunLoopRun + 878
	18  CoreFoundation                      0x00007fff203896d6 CFRunLoopRunSpecific + 567
	19  GraphicsServices                    0x00007fff2c257db3 GSEventRunModal + 139
	20  UIKitCore                           0x00007fff24696cf7 -[UIApplication _run] + 912
	21  UIKitCore                           0x00007fff2469bba8 UIApplicationMain + 101
	22  ---                           0x000000010a26710b main + 75
	23  libdyld.dylib                       0x00007fff2025a3e9 start + 1
	24  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Completion handler passed to -[flutter_inappwebview.InAppWebView webView:didReceiveAuthenticationChallenge:completionHandler:] was not called'

@pichillilorenzo

@pichillilorenzo
Copy link
Owner

pichillilorenzo commented Mar 9, 2021

Using my code example, I can click on the link multiple times without any crash.
It just prints on the console multiple times the createWindowRequest.

@Alexays could you provide a code example with real links, please? Otherwise I cannot reproduce this error, thanks

@Alexays
Copy link

Alexays commented Mar 10, 2021

@pichillilorenzo Here's a reproduction of the crash.
I guess that the crash occur when i load data from onWebViewCreated callback

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();

  if (Platform.isAndroid) {
    await AndroidInAppWebViewController.setWebContentsDebuggingEnabled(true);
  }

  runApp(MaterialApp(home: MyApp()));
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
    crossPlatform:
        InAppWebViewOptions(javaScriptCanOpenWindowsAutomatically: true),
    android: AndroidInAppWebViewOptions(
      supportMultipleWindows: true,
      useHybridComposition: true,
    ),
  );

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  void onWebViewCreated(InAppWebViewController controller) async {
    await controller.loadData(data: ''''
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Flutter InAppWebView</title>
</head>
<body>
  <a style="margin: 50px; background: #333; color: #fff; font-weight: bold; font-size: 20px; padding: 15px; display: block;"
    href="https://flutter.dev"
    target="_blank">
    Click here to open https://github.com/flutter in a popup!
  </a>
</body>
</html>
''', baseUrl: Uri(scheme: 'https', host: 'localhost'));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('On Create Window test')),
      body: SafeArea(
          child: Column(children: <Widget>[
        Expanded(
          child: InAppWebView(
            initialData: InAppWebViewInitialData(
                data: '', baseUrl: Uri(scheme: 'https', host: 'localhost')),
            initialOptions: options,
            onWebViewCreated: onWebViewCreated,
            onCreateWindow: (controller, createWindowRequest) async {
              print("Window ID is: " + createWindowRequest.windowId.toString());
              print(createWindowRequest);
              return true;
            },
          ),
        ),
      ])),
    );
  }
}

@pichillilorenzo
Copy link
Owner

pichillilorenzo commented Mar 11, 2021

@Alexays thanks! I fixed it! It was not related to calling it inside the onWebViewCreated event, but there was another problem.
On iOS, when you try to open a new WebView window, for example, because of target="_blank", it starts the request as soon as the link is clicked. Instead, on Android, it won't start until you create a new WebView window (in this case from the Flutter side).
So, I added on iOS one more flag that is used to block the request itself if the new WebView window is not created yet from the Flutter side.
This fix will be available in the next release.

Copy link

github-actions bot commented Nov 6, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants