[Flutter] Widget정리 #2 WebView, flutter 권한
WebView 정리
https://pub.dev/packages/webview_flutter
4.0 버전이 새로 올라왔는데 기존 3.0과는 사용법이 조금 달라졌다.
달라진점
Instantiating WebViewController
In version 3.0 and below, WebViewController could only be retrieved in a callback after the WebView was added to the widget tree. Now, WebViewController must be instantiated and can be used before it is added to the widget tree. See Usage section above for an example.
Replacing WebView Functionality
The WebView class has been removed and its functionality has been split into WebViewController and WebViewWidget.
WebViewController handles all functionality that is associated with the underlying web view provided by each platform. (e.g., loading a url, setting the background color of the underlying platform view, or clearing the cache).
WebViewWidget takes a WebViewController and handles all Flutter widget related functionality (e.g., layout direction, gesture recognizers).
See the Dartdocs for WebViewController and WebViewWidget for more details.
PlatformView Implementation on Android
The PlatformView implementation for Android is currently no longer configurable. It uses Texture Layer Hybrid Composition on versions 23+ and automatically fallbacks to Hybrid Composition for version 19-23. See https://github.com/flutter/flutter/issues/108106 for progress on manually switching to Hybrid Composition on versions 23+.
API Changes
Below is a non-exhaustive list of changes to the API:
- WebViewController.clearCache no longer clears local storage. Please use WebViewController.clearLocalStorage.
- WebViewController.clearCache no longer reloads the page.
- WebViewController.loadUrl has been removed. Please use WebViewController.loadRequest.
- WebViewController.evaluateJavascript has been removed. Please use WebViewController.runJavaScript or WebViewController.runJavaScriptReturningResult.
- WebViewController.getScrollX and WebViewController.getScrollY have been removed and have been replaced by WebViewController.getScrollPosition.
- WebViewController.runJavaScriptReturningResult now returns an Object and not a String. This will attempt to return a bool or num if the return value can be parsed.
- CookieManager is replaced by WebViewCookieManager.
- NavigationDelegate.onWebResourceError callback includes errors that are not from the main frame. Use the WebResourceError.isForMainFrame field to filter errors.
- The following fields from WebView have been moved to NavigationDelegate. They can be added to a WebView with WebViewController.setNavigationDelegate.
- WebView.navigationDelegate -> NavigationDelegate.onNavigationRequest
- WebView.onPageStarted -> NavigationDelegate.onPageStarted
- WebView.onPageFinished -> NavigationDelegate.onPageFinished
- WebView.onProgress -> NavigationDelegate.onProgress
- WebView.onWebResourceError -> NavigationDelegate.onWebResourceError
- The following fields from WebView have been moved to WebViewController:
- WebView.javascriptMode -> WebViewController.setJavaScriptMode
- WebView.javascriptChannels -> WebViewController.addJavaScriptChannel/WebViewController.removeJavaScriptChannel
- WebView.zoomEnabled -> WebViewController.enableZoom
- WebView.userAgent -> WebViewController.setUserAgent
- WebView.backgroundColor -> WebViewController.setBackgroundColor
- The following features have been moved to an Android implementation class. See section Platform-Specific Features for details on accessing Android platform specific features.
- WebView.debuggingEnabled -> static AndroidWebViewController.enableDebugging
- WebView.initialMediaPlaybackPolicy -> AndroidWebViewController.setMediaPlaybackRequiresUserGesture
- The following features have been moved to an iOS implementation class. See section Platform-Specific Features for details on accessing iOS platform specific features.
- WebView.gestureNavigationEnabled -> WebKitWebViewController.setAllowsBackForwardNavigationGestures
- WebView.initialMediaPlaybackPolicy -> WebKitWebViewControllerCreationParams.mediaTypesRequiringUserAction
- WebView.allowsInlineMediaPlayback -> WebKitWebViewControllerCreationParams.allowsInlineMediaPlayback
-- 4.0
- 최신 버전은 WebView() 사용하지 않고 WebBiewController를 선언하고 WebViewWidget( controller : controller)로 사용
static WebViewController controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(Colors.blueGrey.shade100)
..setNavigationDelegate(
NavigationDelegate(
onProgress: (progress) {},
onPageStarted: (url) {},
onPageFinished: (url) {},
onWebResourceError: (error) {},
onNavigationRequest: (request) {
if (request.url.startsWith('url')) {
return NavigationDecision.prevent;
} else {
return NavigationDecision.navigate;
}
},
),
)
..loadRequest(Uri.parse('url'));
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.indigo[900],
title: Text('title'),
centerTitle: true,
),
body: WebViewWidget(
controller: controller,
));
}
-- 3.0
- WebView를 선언하여 사용
WebViewController? controller;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: //...
body: WebView(
initialUrl : 'url',
javascriptMode : JavascriptMode.unrestricted,
onViewCreted: (WebViewController controller) {
this.controller = controller;
},
));
}
initialUrl
- 웹뷰에서 처음 실행할 웹사이트 문자열로 제공. 웹쥬가 포함된 위젯이 화면에 생성되면 웹뷰가 생성되고 initialUrl에 제공된 사이트가 실행
javascriptMode
- 웹뷰에서 자바스크립트 실행 허용여부
- JavascriptMode.unrestricted : 자바스크립트 제한없이 실행
- JavascripteMode.disabled : 자바스크립트 실행 불가
onWebViewCreated
- 웹뷰 위젯이 생성되면 실행할 콜백 함수. 매개변수로 WebViewController 가 주어져 뒤로가기, 앞으로가기 새로운 URL실행하기 등 기능 조작 가능
onPagedStarted
- 웹뷰가 처음 생성되거나 페이지를 이동했을 때 웹페이지가 로딩되기 시작하면 실행할 콜백 함수를 정의. 매개변수로 로딩이 시작된 페이지의 URL이 문자열로 제공
inPagedFinished
- 웹페이지 로딩이 끝나면 실행. 로딩이 완료된 웹페이지의 URL이 문자열로 제공
onProgress
- 웹페이지가 로딩중일 때 지속적으로 실행되며 페이지의 로딩이 끝날때 실행. 매개변수는 int값으로 페이지 로딩상태를 0부터 100까지 숫자로 제공
권한추가
권한 정리
andriod는 android/app/src/main/build.gradle
iOS 는 info.plist 에 권한을 작성한다. <key> NSCalendarsUsageDescription <string/> 보여줄 메시지 <key/> 방식
Android | iOS |
android.permission.INTERNET - 인터넷 android.permission.CAMERA - 카메라 android.permission.WRITE_EXTERNAL_STROAGE - 외부 파일 저장 android.permission.READ_EXTERNAL_STROAGE - 외부 파일 읽기 android.permission.VIBRATE - 진동 android.permission.ACCESS_FINE_LOCATION - 정확한 위치 android.permission.ACCESS_COARSE_LOCOATION - 네트워크 위치 android.permission.ACCESS_BACKGROUND_LOCATION - 백그라운드에 있을때 위치 체크 android.permission.BILLING - 인앱 결제 android.permission.CALL_PHONE - 전화기 앱 사용 안하고 전화 android.permission.NETWORK_STATE - 네트워크 상태 가져오기 android.permission.RECORED_AUDIO - 음성녹음 |
NSCalendarsUsageDescription - 달력 사용 권한 NSCameraUsageDescription - 카메라 사용 권한 NSContactsUsageDescription - 연락처 사용 권한 NSALocationUsageDescription - 위치정보 사용 권한 NSPhotoLibraryUsageDescription - 사진 접근 권한 NSFaceIDUsageDesription - FaceID사용 권한 NSMicrophoneUsageDescription - 마이크 사용 권한 NSMotionUsageDescription - Accelerometer 사용 권한 NSSiriUsageDesription - Siri사용 권 |
'Study > Flutter' 카테고리의 다른 글
[Flutter] gradle 버전 이슈 (0) | 2023.01.25 |
---|---|
[Flutter] Scaffold 정리 (0) | 2023.01.19 |
[Flutter] StatefulWidget vs StatelessWidget (0) | 2023.01.17 |
[Flutter] Widget정리 #2 배치관련 위젯 (0) | 2023.01.16 |
[Flutter] Widget정리 #1 Text, 제스처, 디자인 (0) | 2023.01.13 |
댓글