Flutter Permission Get 하기!

Jey·2023년 6월 27일

앱을 만들다 보면 권한은 한 번 추가해놓고 다시 건들 일이 없기 때문에 다음에 추가할 때쯤 되면 까먹은 상태인 경우가 많다.

미래의 나를 위해 정리 고고.

사용하는 패키지는 permission_handler

줌 같은 기능의 비디오 채팅 기능을 지원하는 웹뷰를 띄우기 위해서 카메라와 마이크의 권한을 얻어보자.

iOS 환경 설정 🍎

  1. Podfile 에서 사용할 권한들을 추가해준다.
    위에서 첨부한 패키지의 설명 페이지에 보면 주석처리 된 권한 코드를 복사할 수 있는데, 그 중 사용하고 싶은 권한만 아래와 같이 주석을 풀어주면 된다.
post_install do |installer|
  installer.pods_project.targets.each do |target|
    ... # Here are some configurations automatically generated by flutter
  
    # Start of the permission_handler configuration
    target.build_configurations.each do |config|
   
      # You can enable the permissions needed here. For example to enable camera
      # permission, just remove the `#` character in front so it looks like this:
      #
      # ## dart: PermissionGroup.camera
      # 'PERMISSION_CAMERA=1'
      #
      #  Preprocessor definitions can be found in: https://github.com/Baseflow/flutter-permission-handler/blob/master/permission_handler_apple/ios/Classes/PermissionHandlerEnums.h
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
        '$(inherited)',
  
        ## dart: PermissionGroup.calendar
        # 'PERMISSION_EVENTS=1',
  
        ## dart: PermissionGroup.camera
        'PERMISSION_CAMERA=1',
  
        ## dart: PermissionGroup.microphone
        'PERMISSION_MICROPHONE=1',
      ]
  
    end 
    # End of the permission_handler configuration
  end
end

2.Info.plist에 권한설명을 추가한다.

  1. Clean & Rebuild
flutter clean && flutter pub get
cd ios
pod install
cd..
flutter run

Andr 환경 설정 🤖

  • Android X의 경우 패키지 설명( permission_handler )에 추가 설명이 있으니 참조하길 바란다.

환경설정이 끝났다면 권한이 필요한 코드로 가서 각 퍼미션 뒤에 .request() 를 붙여 권한을 요청한다.

_checkCameraPermission() async {
	var status = await Permission.camera.status;
	if (status.isDenied) {
  	// We didn't ask for permission yet or the permission has been denied before but not permanently.
    	Permission.camera.request();
	}

}

_checkMicrophoneermission() async {
	var status = await Permission.microphone.status;
	if (status.isDenied) {
  	// We didn't ask for permission yet or the permission has been denied before but not permanently.
    	Permission.microphone.request();
	}

}
profile
✨flutter, ios developer✨

0개의 댓글