์น๋ทฐ๋?
์ฑ์์ ์น๋ธ๋ผ์ฐ์ ๊ธฐ๋ฅ์ ๊ตฌํํด์ฃผ๋ ๊ธฐ์
๋ณด์ฌ์ฃผ๋ ํ๋ฉด์ ์น์ผ๋ก ๊ฐ๋ฐํด๋๊ณ ๊ฒ์ ์ฑ์ผ๋ก ๊ฐ์ธ์ ๋ฐฐํฌํจ
๋ค์ดํฐ๋ธ์ ๋นํด ์๋๊ฐ ๋๋ฆฌ๊ณ , ์ ๋๋ฉ์ด์ ์ด ๋ถ์์ฐ์ค๋ฝ๊ธด ํ์ง๋ง ๊ธฐ์กด์ ์น์ฌ์ดํธ๋ฅผ ์ฌํ์ฉํ ์ ์์.
0. ์ฌ์ ์ค์
(1) pubspec.yaml ์ค์
webview_flutter ์์กด์ฑ ์ถ๊ฐ
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
webview_flutter: 3.0.4
(2) ์๋๋ก์ด๋ ์ค์
android/app/src/main/AndroidManifest.xml
uses-permission, android:usesCleartextTraffic ์ถ๊ฐ
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.blog_web_app">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:label="blog_web_app"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher"
android:usesCleartextTraffic="true">
.. ์๋ต
build.gradle ์ค์
android/app/build.gradle
compiledSdkVersion, minSdkVersion ์์
android {
compileSdkVersion 32 //32๋ก ์์
... ์๋ต ...
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.blog_web_app"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdkVersion 20 //20์ผ๋ก ๋ณ๊ฒฝ
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
(3) ios ์ค์
ios/Runner/Info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
... ์๋ต ...
<!-- ์๋ ๋ด์ฉ ์ถ๊ฐ >
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsLocaleNetworking</key>
<true/>
<key>NSAllowsArbitraryLoadsInWebContent</key>
</dict>
</dict>
</plist>
1. ์ฒซ ํ๋ฉด ๋์ฐ๊ธฐ
class HomeScreen extends StatelessWidget {
final url = 'https://yonglimlee.tistory.com';
HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.orange,
title: Text('yllee'),
centerTitle: true, //๊ฐ์ด๋ฐ ์ ๋ ฌ
),
body: WebView(
initialUrl: url,
javascriptMode: JavascriptMode.unrestricted,
),
);
}
}
์คํฌ๋กค์ ํด๋ณด๋ฉด ์ ๋๋ก ๋์ํจ์ ํ์ธํ ์ ์๋ค.
2. ์น๋ทฐ ์ปจํธ๋กค๋ฌ ์ถ๊ฐ
ํ๋ฌํฐ๋ ์์ ฏ์ ํ๋ก๊ทธ๋จ์ ์ผ๋ก(์ฝ๋๋ก) ์ ์ดํ๋ ๊ธฐ๋ฅ์ ์ ๊ณต. ์ด ๊ธฐ๋ฅ์ ์ปจํธ๋กค๋ฌ๊ฐ ๋ด๋นํ๊ฒ ๋๋๋ฐ ์น๋ทฐ์ ๊ฒฝ์ฐ์๋ ์น๋ทฐ ์ปจํธ๋กค๋ฌ๋ฅผ ์ด์ฉํด ์ ์ดํ ์ ์๋ค.
class HomeScreen extends StatelessWidget {
/*์ถ๊ฐ๋จ*/
WebViewController? controller;
final url = 'https://yonglimlee.tistory.com';
HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.orange,
title: Text('yllee'),
centerTitle: true, //๊ฐ์ด๋ฐ ์ ๋ ฌ
/*์ถ๊ฐ๋จ*/
actions: [
IconButton(
onPressed: (){
if(controller != null){
controller!.loadUrl(url); //ํ๋ฒํผ์ ๋๋ฅด๋ฉด ์ด๊ธฐ url๋ก ์ด๋
}
},
icon: const Icon(Icons.home)
),
IconButton(
onPressed: (){
if(controller != null){
controller!.goBack();
}
},
icon: const Icon(Icons.arrow_back) //๋ค๋ก๊ฐ๊ธฐ ๋ฒํผ์ ๋๋ฅด๋ฉด ์ด์ ํ์ด์ง๋ก ์ด๋
),
IconButton(
onPressed: (){
if(controller != null){
controller!.goForward();
}
},
icon: const Icon(Icons.arrow_forward)//์์ผ๋ก๊ฐ๊ธฐ ๋ฒํผ์ ๋๋ฅด๋ฉด ์ ํ์ด์ง๋ก ์ด๋
)
],
),
body: WebView(
initialUrl: url,
javascriptMode: JavascriptMode.unrestricted,
/*์ถ๊ฐ๋จ. ์น๋ทฐ ์์ฑํจ์*/
onWebViewCreated: (WebViewController controller){
this.controller = controller; //๋ณ์์ ์ ์ฅํ๋ฉด ์์ ฏ ์ด๋์๋ ์ฌ์ฉ๊ฐ๋ฅํจ
},
),
);
}
}
๋๊ธ