seong
Flutter -권한 요청 iOS PermissionHandler 본문
사용 라이브러리
https://pub.dev/packages/permission_handler
1. pubspec.yaml 라이브러리 추가
dependencies:
flutter:
sdk: flutter
permission_handler: ^11.3.1
cupertino_icons: ^1.0.6
2. 라이브러리 설명서 대로 ios/Podfile에 아래 코드 추가
- 사용 해야할 권한을 주석을 풀어주고 0 -> 1 로 바꿔주면 된다
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
# You can remove unused permissions here
# for more information: https://github.com/BaseflowIT/flutter-permission-handler/blob/master/permission_handler/ios/Classes/PermissionHandlerEnums.h
# e.g. when you don't need camera permission, just add 'PERMISSION_CAMERA=0'
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)',
## dart: PermissionGroup.calendar
'PERMISSION_EVENTS=0',
## dart: PermissionGroup.calendarFullAccess
'PERMISSION_EVENTS_FULL_ACCESS=0',
## dart: PermissionGroup.reminders
'PERMISSION_REMINDERS=0',
## dart: PermissionGroup.contacts
'PERMISSION_CONTACTS=0',
## dart: PermissionGroup.camera
'PERMISSION_CAMERA=1',
## dart: PermissionGroup.microphone
'PERMISSION_MICROPHONE=1',
## dart: PermissionGroup.speech
'PERMISSION_SPEECH_RECOGNIZER=0',
## dart: PermissionGroup.photos
'PERMISSION_PHOTOS=1',
## The 'PERMISSION_LOCATION' macro enables the `locationWhenInUse` and `locationAlways` permission. If
## the application only requires `locationWhenInUse`, only specify the `PERMISSION_LOCATION_WHENINUSE`
## macro.
##
## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
'PERMISSION_LOCATION=1',
'PERMISSION_LOCATION_WHENINUSE=0',
## dart: PermissionGroup.notification
'PERMISSION_NOTIFICATIONS=1',
## dart: PermissionGroup.mediaLibrary
'PERMISSION_MEDIA_LIBRARY=0',
## dart: PermissionGroup.sensors
'PERMISSION_SENSORS=0',
## dart: PermissionGroup.bluetooth
'PERMISSION_BLUETOOTH=1',
## dart: PermissionGroup.appTrackingTransparency
'PERMISSION_APP_TRACKING_TRANSPARENCY=0',
## dart: PermissionGroup.criticalAlerts
'PERMISSION_CRITICAL_ALERTS=0',
## dart: PermissionGroup.criticalAlerts
'PERMISSION_ASSISTANT=0',
]
end
end
end
3. 간단하게 테스트 할 코드 작성
버튼을 누르면 권한을 요청하고, 허용이 되었는지를 확인
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({
super.key,
});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String? locationPermission;
String? camera;
String? photos;
String? microPhone;
String? bluetooth;
String? notification;
Future<void> locationStatus() async {
PermissionStatus status = await Permission.location.status;
locationPermission = status.toString();
}
Future<void> cameraStatus() async {
PermissionStatus status = await Permission.camera.status;
camera = status.toString();
}
Future<void> photosStatus() async {
PermissionStatus status = await Permission.photos.status;
photos = status.toString();
}
Future<void> microPhoneStatus() async {
PermissionStatus status = await Permission.microphone.status;
microPhone = status.toString();
}
Future<void> bluetoothStatus() async {
PermissionStatus status = await Permission.bluetooth.status;
bluetooth = status.toString();
}
Future<void> notificationStatus() async {
PermissionStatus status = await Permission.notification.status;
notification = status.toString();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_buildWidget('위치 권한: $locationPermission', '위치 권한', () {
setState(
() {
Future.wait([
Permission.location.request(),
locationStatus(),
]);
},
);
}),
_buildWidget('카메라 권한: $camera', '카메라 권한', () {
setState(
() {
Future.wait([
Permission.camera.request(),
cameraStatus(),
]);
},
);
}),
_buildWidget('갤러리 권한: $photos', '갤러리 권한', () {
setState(
() {
Future.wait([
Permission.photos.request(),
photosStatus(),
]);
},
);
}),
_buildWidget('마이크 권한: $microPhone', '마이크 권한', () {
setState(
() {
Future.wait([
Permission.microphone.request(),
microPhoneStatus(),
]);
},
);
}),
_buildWidget('블루투스 권한: $bluetooth', '블루투스 권한', () {
setState(
() {
Future.wait([
Permission.bluetooth.request(),
bluetoothStatus(),
]);
},
);
}),
_buildWidget('알림 권한: $notification', '알림 권한', () {
setState(
() {
Future.wait([
Permission.notification.request(),
notificationStatus(),
]);
},
);
}),
],
),
);
}
Widget _buildWidget(
String permissionStatus, String buttonText, VoidCallback onPressed) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
width: double.infinity,
child: Text(
permissionStatus,
textAlign: TextAlign.center,
),
),
ElevatedButton(onPressed: onPressed, child: Text(buttonText)),
SizedBox(height: 20),
],
);
}
}
3. 터미널에서 "open ios/Runner.xcworkspace" 입력후 info.plist에 아래 처럼 사용할 권한 추가
- 만약 아래 처럼 에러가 나온다면, info.plist에 추가 되지 않았다는 말이다, 추가 해주면 된다.
'Flutter > Flutter' 카테고리의 다른 글
Flutter - flutter_callkit_incoming 라이브러리 사용 ( Fake_call ) (0) | 2024.09.25 |
---|---|
Flutter - Android 뒤로가기 버튼 클릭을 홈버튼과 동일한 효과 내기 (0) | 2024.09.19 |
Flutter - BottomNav 더블 탭시 화면의 스크롤 가장 위로 보내기 (0) | 2024.08.09 |
Flutter - 사용자 설정 폰트 크기 무시하기 (0) | 2024.07.08 |
Flutter - Flutter with Python (2) 파이썬 결과 값 View로 보여주기 (5) | 2024.05.17 |