300x250

url_launcher
패키지를 사용하면 Flutter 앱에서 웹 브라우저, 이메일, 전화, SMS 등 다양한 앱을 실행할 수 있어 크로스 플랫폼 애플리케이션 개발에 필수 요소
url_launcher란?
url_launcher
는 Flutter에서 URL을 통해 외부 애플리케이션(웹 브라우저, 전화, 이메일 등)을 실행할 수 있도록 도와주는 패키지. 이 패키지를 사용하면 간단한 코드로 다양한 플랫폼에서 앱 간 상호작용을 구현할 수 있다.
설치 방법
- 프로젝트에
url_launcher
패키지를 추가하려면 아래 명령어를 실행.flutter pub add url_launcher
- 설치 후
pubspec.yaml
파일에 추가된 의존성을 확인하고 프로젝트를 다시 빌드.
앱 설정
url_launcher
를 사용하려면 Android와 iOS에서 각각 설정이 필요해.
1. Android 설정 (AndroidManifest.xml)
android/app/src/main/AndroidManifest.xml
파일을 열어서 <queries>
태그를 추가해야 해. 이 태그는 앱이 특정 URL 스키마를 처리할 수 있도록 선언하는 역할을 해.
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<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 app 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>
</manifest>
2. iOS 설정 (Info.plist)
iOS에서는 Info.plist
파일에 URL 스키마를 추가해야 해.
<key>LSApplicationQueriesSchemes</key>
<array>
<string>https</string>
<string>http</string>
<string>sms</string>
<string>tel</string>
</array>
기본 사용법
1. URL 열기
아래는 웹 브라우저에서 URL을 여는 간단한 예제야.
import 'package:url_launcher/url_launcher.dart';
final Uri _url = Uri.parse('https://example.com');
Future<void> _launchURL() async {
if (await canLaunchUrl(_url)) {
await launchUrl(_url);
} else {
throw Exception('Could not launch $_url');
}
}
2. 전화 걸기
전화 앱을 실행하려면 tel:
스키마를 사용하면 돼.
final Uri _phoneUrl = Uri.parse('tel:+1234567890');
Future<void> _makePhoneCall() async {
if (await canLaunchUrl(_phoneUrl)) {
await launchUrl(_phoneUrl);
} else {
throw Exception('Could not launch $_phoneUrl');
}
}
3. 이메일 보내기
이메일 앱 실행은 mailto:
스키마로 가능해.
final Uri _emailUrl = Uri(
scheme: 'mailto',
path: 'example@example.com',
query: 'subject=Hello&body=How are you?',
);
Future<void> _sendEmail() async {
if (await canLaunchUrl(_emailUrl)) {
await launchUrl(_emailUrl);
} else {
throw Exception('Could not launch $_emailUrl');
}
}
4. SMS 보내기
SMS 전송은 sms:
스키마를 사용해 구현할 수 있어.
final Uri _smsUrl = Uri.parse('sms:+1234567890');
Future<void> _sendSMS() async {
if (await canLaunchUrl(_smsUrl)) {
await launchUrl(_smsUrl);
} else {
throw Exception('Could not launch $_smsUrl');
}
}
예제 UI 코드
아래는 여러 버튼을 통해 다양한 URL 스키마를 실행하는 Flutter UI 예제야.
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('URL Launcher Example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () async {
final Uri url = Uri.parse('https://example.com');
if (await canLaunchUrl(url)) {
await launchUrl(url);
} else {
print("Can't launch $url");
}
},
child: Text('Open Web Page'),
),
ElevatedButton(
onPressed: () async {
final Uri phone = Uri.parse('tel:+1234567890');
if (await canLaunchUrl(phone)) {
await launchUrl(phone);
} else {
print("Can't make a call");
}
},
child: Text('Make Phone Call'),
),
ElevatedButton(
onPressed: () async {
final Uri email = Uri(
scheme: 'mailto',
path: 'example@example.com',
query: 'subject=Hello&body=How are you?',
);
if (await canLaunchUrl(email)) {
await launchUrl(email);
} else {
print("Can't send email");
}
},
child: Text('Send Email'),
),
ElevatedButton(
onPressed: () async {
final Uri sms = Uri.parse('sms:+1234567890');
if (await canLaunchUrl(sms)) {
await launchUrl(sms);
} else {
print("Can't send SMS");
}
},
child: Text('Send SMS'),
),
],
),
),
),
);
}
}
이 글에서는 url_launcher
패키지를 사용하여 다양한 URL 스키마를 처리하고 외부 애플리케이션과 상호작용하는 방법을 다뤄쓴데, 관련해 궁금한거나 고쳐야할 부분, 추가 꿀팁은 댓글로 부탁드립니다~
300x250
'Flutter.Dart > Flutter 패키지 추천' 카테고리의 다른 글
[Flutter 추천 패키지] Flutter OSS Licenses (0) | 2025.01.19 |
---|