dev/flutter
[Flutter] Permission
donguran
2024. 1. 14. 08:36
728x90
반응형
Native Setting
- aos
path : android/app/src/main/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>
- ios
path : 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>NSCameraUsageDescription</key>
<string>request camera granted</string>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
</dict>
</plist>
Flutter setting
- flutter
path : puspec.yaml (or terminal)
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
permission_handler: ^x.x.x
# terminal
flutter pub add permission_handler
https://pub.dev/packages/permission_handler
permission_handler | Flutter Package
Permission plugin for Flutter. This plugin provides a cross-platform (iOS, Android) API to request and check permissions.
pub.dev
Use permission_handler library
import
import 'package:permission_handler/permission_handler.dart';
void requestPermission() async {
final permission = await Permission.camera.request();
print('what response string? permission:$permission');
if (permission == PermissionStatus.granted) {
print('permission granted success');
} else {
print('permission denied or cancel');
}
}
Future<bool> init() async {
final Map<Permission, PermissionStatus> response = await [Permission.camera, Permission.microphone].request();
final cameraPermission = response[Permission.camera];
final micPermission =response[Permission.microphone];
if (cameraPermission != PermissionStatus.granted || micPermission != PermissionStatus.granted) {
throw '카메라 또는 마이크 권한이 없습니다';
}
return true;
}
FutureBuilder
this Widget can wait receive return type of Future.
if Future type function receive data.
so FutureBuilder execute build method.
changed data return argument snapshot.
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('LIVE'),
),
body: FutureBuilder(
future: init(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Center(
child: Text(snapshot.error.toString()),
);
}
// receive data not yet.
if (!snapshot.hasData) {
return const Center(
child: CircularProgressIndicator(),
);
}
return Center(
child: Text('모든 권한이 있다.'),
);
},
),
);
}
2024-05-28-화 Permission 허용안함 및 취소시 시스템 설정으로 이동하기
add
flutter pub add app_settings
use
await AppSettings.openAppSettings();
더보기
예시코드
bool isRequest = await showDialog(context: context, builder: (context) => AlertDialog(
title: Text("알림".tr()),
content: Text("알림 권한을 허용해주세요"),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: Text("취소".tr(), style: const TextStyle(color: Colors.redAccent),),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: Text("이동".tr(), style: const TextStyle(color: Colors.blueAccent),),
)
],
),) ?? false;
if (isRequest) {
await AppSettings.openAppSettings();
if (await alarmProvider.requestPermission) {
timeOfDate = await showDialog(
context: context,
builder: (context) => const PlanTimePickerDialog(),
);
if (timeOfDate != null) {
Log.s("timeOfDate:$timeOfDate");
isAlarm = true;
}
}
}
2024-07-05-금 : PhotoManager.requestPermissionExtend()하여 권한을 요청했을시
제한된 액세스 허용 :
뒤로가기시 > PermissionState.denied
허용안함 클릭시 > PermissionState.limited
스크롤을 아래로 내리면 > PermissionState.denied
모두 허용: limited (이상하다. 모두 허용했는데 limited라니)
허용안함 : denied
이번만 허용 : limited
728x90
반응형