Skip to content

Restart app in safari #2686

@Arshad-ullah

Description

@Arshad-ullah

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

The Flutter web app works perfectly on Android, iOS, and Chrome. However, when opening the game in Safari, the web app automatically restarts

Expected Behavior

The Flutter web app should not restart when the game is opened in Safari.

Steps with code example to reproduce

Steps with code example to reproduce
class WebviewScreen extends StatefulWidget {
  String? url;

  String? gameName;
  String? gameUniqueCode;
  String? gameCode;
  bool isFromActivity;
  String hallGame;

  WebviewScreen({
    super.key,
    this.url,
    this.gameCode,
    this.gameName,
    this.gameUniqueCode,
    this.hallGame = "0",
    this.isFromActivity = false,
  });

  @override
  State<WebviewScreen> createState() => _WebviewScreenState();
}

class _WebviewScreenState extends State<WebviewScreen> {
  InAppWebViewController? _webViewController;

  bool isWebviweloading = false;
  bool isFullScreen = false;

  init() {
    if (!kIsWeb) {
      // if (widget.gameUniqueCode == "5001" || widget.gameUniqueCode == "5002") {
      //   _enableAllOrientations();
      // }
      if (!widget.isFromActivity) {
        if (widget.gameUniqueCode![0] == "5" ||
            widget.gameUniqueCode![0] == "4") {
          _enableAllOrientations();
        }
      }
    }
  }

  void appBack({bool landscape = false}) {
    if (!kIsWeb) {
      if (widget.isFromActivity) {
        Utils.pop();
      } else {
        showCloseDialog(islandscape: landscape);
      }
    } else {
      Utils.pop();
    }
  }

  @override
  void initState() {
    init();

    super.initState();
  }

  @override
  void dispose() {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);

    super.dispose();
  }

  // late final WebViewController controller;
  final bool _canPop = true;
  var appBarHeight = AppBar().preferredSize.height;

  late DateTime _loadStartTime;

  @override
  Widget build(BuildContext context) {
    print("url..55..${widget.url}");
    return OrientationBuilder(builder: (context, orientation) {
      return PopScope(
        canPop: _canPop,
        onPopInvokedWithResult: (didPop, result) {
          if (!didPop) {
            appBack();
          }
        },
        child: Scaffold(
          appBar: isFullScreen
              ? null
              : (orientation == Orientation.portrait)
                  ? portraitAppBar()
                  : landscapeAppBar(),
          body: SafeArea(
            bottom: true,
            top: false,
            right: false,
            left: false,
            child: Stack(
              alignment: Alignment.topCenter,
              clipBehavior: Clip.none,
              children: [
                InAppWebView(
                  initialUrlRequest: URLRequest(
                    url: WebUri.uri(
                      Uri.parse(widget.url!),
                    ),
                  ),
                  initialSettings: InAppWebViewSettings(
                    javaScriptEnabled: true,
                  ),
                  onReceivedServerTrustAuthRequest:
                      (controller, challenge) async {
                    return ServerTrustAuthResponse(
                      action: ServerTrustAuthResponseAction.PROCEED,
                    );
                  },
                  onWebViewCreated: (InAppWebViewController controller) async {
                    _webViewController = controller;

                    if (widget.isFromActivity) {
                      try {
                        if (!kIsWeb &&
                            (defaultTargetPlatform == TargetPlatform.android ||
                                defaultTargetPlatform == TargetPlatform.iOS)) {
                          await _webViewController?.clearCache();
                          await CookieManager().deleteAllCookies();
                        }
                      } catch (e) {
                        log("⚠️ Error clearing cache: $e");
                      }
                    }
                  },
                  onConsoleMessage: (controller, consoleMessage) {
                    log('on method....$consoleMessage');
                  },
                  onLoadError: (controller, url, code, message) {
                    Toasts.showToast(text: "Error loading page: $message");
                  },
                  onLoadHttpError: (controller, url, statusCode, description) {
                    Toasts.showToast(
                        text: "HTTP error $statusCode: $description");
                  },
                  onReceivedError: (controller, request, error) {
                    log('on method.....onReceiverError');
                    isWebviweloading = false;

                    setState(() {});
                  },
                  onLoadStart: (controller, url) {
                    isWebviweloading = true;
                    _loadStartTime = DateTime.now();
                    setState(() {});
                  },
                  onLoadStop: (controller, url) async {
                    String? token = AppConstant.authToken;
                    await _webViewController!.evaluateJavascript(
                        source: '''transferConsultResult('$token')''');

                    isWebviweloading = false;

                    // final loadDuration = DateTime.now()
                    //     .difference(_loadStartTime)
                    //     .inMilliseconds;
                    // if (loadDuration > 3000) {
                    //   Toasts.showToast(
                    //       text:
                    //           "The user's network is unstable. If it affects the game experience, you can try to refresh manually");
                    // }

                    setState(() {});
                  },
                ),

                // ],
                if (isFullScreen) ...[
                  Container(
                      height: 50.h,
                      width: 50.w,
                      margin: EdgeInsets.only(top: statusBarHeight.h),
                      padding: const EdgeInsets.all(5),
                      decoration: const BoxDecoration(
                        color: kGoldenE58E27,
                        borderRadius: BorderRadius.only(
                            bottomLeft: Radius.circular(5),
                            bottomRight: Radius.circular(5)),
                      ),
                      child: PointerInterceptor(
                        child: GestureDetector(
                          onTap: () {
                            log('webview page fullscreen button click');
                            isFullScreen = false;
                            setState(() {});
                          },
                          child: ImageIcon(
                            AssetImage(
                              Images.squareArrowDown,
                            ),
                            color: kWhite,
                            size: 20,
                          ),
                        ),
                      )),
                ],
                if (isWebviweloading) const Center(child: LoadingIndicator()),
              ],
            ),
          ),
        ),
      );
    });
  }

This is my code I just pass game url .

Stacktrace/Logs

Stacktrace/Logs
<Replace this line by pasting your stacktrace or logs here>

Flutter version

v3.29.3

Operating System, Device-specific and/or Tool

Iphone 8, safari

Plugin version

v6.1.5

Additional information

https://drive.google.com/file/d/1MjsGWzUyrT5zYalKVKDlY40DWYjBNahw/view?usp=sharing

Video also recorded .

Please Identify the issue why in safari inappwebview restart the app .

Thanks

Self grab

  • I'm ready to work on this issue!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions