본문 바로가기

Flutter

[Flutter Favorite] url_launcher web, phone, sms, email 예시코드

반응형

Version

// pubspec.yaml에 추가합니다. 포스팅 당시 제가 사용한 버전은 6.1.4 입니다. 
url_launcher: ^6.1.4

 

Android 설정

android - app - src - main - AndroidManifest.xml 

 <queries>
    <!-- If your app opens https URLs -->
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="https" />
    </intent>
    <!-- If your app makes calls -->
    <intent>
        <action android:name="android.intent.action.DIAL" />
        <data android:scheme="tel" />
    </intent>
    <!-- If your sends SMS messages -->
    <intent>
        <action android:name="android.intent.action.SENDTO" />
        <data android:scheme="smsto" />
    </intent>
    <!-- If your app sends emails -->
    <intent>
        <action android:name="android.intent.action.SEND" />
        <data android:mimeType="*/*" />
    </intent>
</queries>

 

ios 설정

<array>
	<string>sms</string>
	<string>tel</string>
	<string>mailto</string>
	<string>https</string>
	<string>https</string>
</array>

 

WebView 코드

  _openURL(Uri url) async {
    if (!await launchUrl(
      url,
      mode: LaunchMode.inAppWebView,
      webViewConfiguration: const WebViewConfiguration(
          headers: <String, String>{'my_header_key': 'my_header_value'}),
    )) {
      //     throw 'Could not launch $url';
    }
  }

 

Phone 코드 

  _makingPhoneCall() async {
    var url = Uri.parse("tel:01011111111");
    if (await canLaunchUrl(url)) {
      await launchUrl(url);
    } else {
      throw 'Could not launch url $url';
    }
  }

 

Email 코드

  _sendingMails() async {
    var url = Uri.parse("mailto:kimiszero@test.com");
    if (await canLaunchUrl(url)) {
      await launchUrl(url);
    } else {
      throw 'Could not launch mail url $url';
    }
  }

 

SMS 코드

 _sendingSMS() async {
    var url = Uri.parse("sms:966738292");
    if (await canLaunchUrl(url)) {
      await launchUrl(url);
    } else {
      throw 'Could not launch sms url $url';
    }
  }

 

구현 화면

 

참고로 ios 시뮬레이터에서 phone, sms, mail은 테스트 되지 않습니다.

실기기를 사용하시거나 android 로 테스트를 진행해보시면 될 것 같습니다. 아직 테스트 못한 코드입니다. 🥲 

반응형