Flutter InAppWebView Err more

intent:// URL 스키마는 Android에서 특정 앱을 실행하기 위한 인텐트(Intent) 형식입니다. 그러나 Flutter InAppWebView는 기본적으로 이러한 URL을 처리하지 못하여 net::ERR_UNKNOWN_URL_SCHEME 오류가 발생할 수 있습니다.

이를 해결하려면 shouldOverrideUrlLoading을 사용하여 intent:// 스키마를 감지한 후, 이를 url_launcher 패키지를 통해 외부 앱에서 실행하도록 처리해야 합니다.


🔹 해결 방법: shouldOverrideUrlLoading에서 intent:// 처리

아래와 같이 intent:// URL을 감지하여 launchUrl로 실행하면 문제를 해결할 수 있습니다.

📌 수정된 코드

import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:url_launcher/url_launcher.dart';

void main() {
  runApp(MyApp());
}

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

class _MyAppState extends State<MyApp> {
  InAppWebViewController? webViewController;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text("InAppWebView Example")),
        body: InAppWebView(
          initialUrlRequest: URLRequest(url: WebUri("https://example.com")),
          onWebViewCreated: (controller) {
            webViewController = controller;
          },
          shouldOverrideUrlLoading: (controller, navigationAction) async {
            var uri = navigationAction.request.url;

            if (uri == null) {
              return NavigationActionPolicy.ALLOW;
            }

            String url = uri.toString();

            // 🔹 intent:// URL 처리
            if (url.startsWith("intent://")) {
              try {
                String intentUrl = url.replaceFirst("intent://", "https://"); // 대체 URL 설정 (필요한 경우)
                if (await canLaunchUrl(Uri.parse(intentUrl))) {
                  await launchUrl(Uri.parse(intentUrl));
                  return NavigationActionPolicy.CANCEL;
                } else {
                  print("Could not launch $intentUrl");
                }
              } catch (e) {
                print("Error launching intent: $e");
              }
              return NavigationActionPolicy.CANCEL;
            }

            // 🔹 market://, tel:// 등 기타 스키마 처리
            if (!url.startsWith("http") && !url.startsWith("https")) {
              try {
                if (await canLaunchUrl(Uri.parse(url))) {
                  await launchUrl(Uri.parse(url));
                  return NavigationActionPolicy.CANCEL;
                }
              } catch (e) {
                print("Could not launch $url: $e");
              }
            }

            return NavigationActionPolicy.ALLOW;
          },
        ),
      ),
    );
  }
}

🔹 설명

  1. shouldOverrideUrlLoading에서 intent://로 시작하는 URL을 감지합니다.
  2. intent://https://로 변환할 수도 있지만, 대부분의 경우 launchUrl을 통해 직접 실행하면 됩니다.
  3. launchUrl을 사용하여 외부 앱에서 실행합니다.
  4. NavigationActionPolicy.CANCEL을 반환하여 웹뷰가 오류를 발생시키지 않도록 합니다.
  5. 기타 market://, tel:// 등의 URL도 함께 처리하여 앱이 크래시되지 않도록 합니다.

🔹 필요한 패키지 추가

먼저, pubspec.yamlflutter_inappwebviewurl_launcher 패키지를 추가하세요.

dependencies:
  flutter:
    sdk: flutter
  flutter_inappwebview: ^6.0.0
  url_launcher: ^6.0.9

이후 flutter pub get을 실행하여 패키지를 설치합니다.


intent://로 시작하는 URL도 정상적으로 실행될 것입니다.

Leave a Reply

Your email address will not be published. Required fields are marked *