1. 플러그인 설치
$ flutter pub add url_launcher
$ flutter pub add webview_flutter
2. 플랫폼별 설정
외부 앱을 실행하려면 플랫폼별로 해당하는 파일에 앱 실행 허용 목록을 설정해야 합니다.
또한 외부 앱 실행 후 다시 기존 서비스 앱으로 돌아오고싶다면 서비스하는 앱 스킴 또한 등록해주어야 합니다.
kakao developers | 커스텀 URL 스킴 설정 방법 참고
url_launcher | Flutter Package
Android 설정 (AndroidManifest.xml)
<!-- Provide required visibility configuration for API level 30 and above -->
<queries>
<!-- If your app checks for SMS support -->
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="sms" />
</intent>
<!-- If your app checks for call support -->
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="tel" />
</intent>
</queries>
iOS 설정 (info.plist)
<key>LSApplicationQueriesSchemes</key>
<array>
<string>sms</string>
<string>tel</string>
</array>
3. 웹뷰에서 외부 앱 실행
// WebViewController
WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setNavigationDelegate(
NavigationDelegate(
onProgress: (int progress) {
debugPrint('WebView is loading (progress : $progress%)');
},
onPageStarted: (String url) {
debugPrint('Page started loading: $url');
},
onPageFinished: (String url) {
debugPrint('Page finished loading: $url');
},
onWebResourceError: (error) {
debugPrint('''
Page resource error:
code: ${error.errorCode}
description: ${error.description}
errorType: ${error.errorType}
isForMainFrame: ${error.isForMainFrame}
''');
},
onNavigationRequest: (request) {
if (request.url.startsWith('http:') ||
request.url.startsWith('https:')) {
debugPrint('allowing navigation to ${request.url}');
return NavigationDecision.navigate;
}
debugPrint('blocking navigation to ${request.url}');
_launchUrl(request.url);
return NavigationDecision.prevent;
},
onUrlChange: (UrlChange change) {
debugPrint('url change to ${change.url}');
},
),
)
...
Future<void> _launchUrl(String url) async {
if (!await launchUrl(
Uri.parse(url),
// mode: LaunchMode.externalApplication,
)) {
throw Exception('Could not launch $url');
}
}
'Flutter' 카테고리의 다른 글
기본 compileSDKVersion, minSDKVersion, targetSDKVersion 정의 된 곳 (mac) (0) | 2023.07.04 |
---|