How to add camera permissions into flutter project?
Asked Answered
K

6

7

I want to open the camera by clicking a button but cannot add the camera and gallery permissions in the iOS package.

I have those keys:

NSPhotoLibraryUsageDescription
NSCameraUsageDescription
NSMicrophoneUsageDescription

And I don't know where I should put those lines.

Krisha answered 17/12, 2019 at 13:2 Comment(1)
Did my answer help?Magus
L
20

edit this file:

[Flutter Project Folder]/ios/Runner/Info.plist

add these entries:

<key>NSPhotoLibraryUsageDescription</key>
<string>App needs access to photo lib for profile images</string>

<key>NSCameraUsageDescription</key>
<string>To capture profile photo please grant camera access</string>

the string part can be any description you want the user to see as an explanation for why your app needs these privileges

these lines should be added after the tag

there is a good elaboration for other options here: https://mcmap.net/q/125794/-nsphotolibraryusagedescription-key-must-be-present-in-info-plist-to-use-camera-roll

Lilylivered answered 30/10, 2020 at 14:15 Comment(0)
M
2

To add permissions in Flutter, you must add permissions to both the AndroidManifest.xml file and the info.plist file.

To add the specific permissions you are asking to the info.plist, you can go to:

iOS -> Runner -> info.plist

enter image description here

Magus answered 17/12, 2019 at 13:7 Comment(0)
S
1

Take a look at the example project for the camera package: https://github.com/flutter/plugins/blob/master/packages/camera/camera/example/ios/Runner/Info.plist

You'll see that those lines are in with the rest of them.

Swacked answered 17/12, 2019 at 13:5 Comment(0)
L
1
  1. Add permission in ios/Runner/info.plist

    <key>NSPhotoLibraryUsageDescription</key>
    <string>This app needs image library access to select invoice images</string>
    
<key>NSCameraUsageDescription</key> <string>This app requires camera access for QR scans and invoice uploads</string>
  1. Change ios/Podfile
post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)
    target.build_configurations.each do |config|
            config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
               '$(inherited)',

               ## dart: PermissionGroup.calendar
               ##'PERMISSION_EVENTS=1',

               ## 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',

               ## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
               ##'PERMISSION_LOCATION=1',

               ## dart: PermissionGroup.notification
               ##'PERMISSION_NOTIFICATIONS=1',

               ## dart: PermissionGroup.appTrackingTransparency
                ##'PERMISSION_APP_TRACKING_TRANSPARENCY=1',

               ## dart: PermissionGroup.mediaLibrary
                'PERMISSION_MEDIA_LIBRARY=1',

               ## dart: PermissionGroup.sensors
               #'PERMISSION_SENSORS=0'
             ]
    end
  end
end
  1. Delete Podfile.lock
  2. Delete pubspec.lock
  3. Clean everything
pod cache clean —all
flutter clean
flutter pub get
(cd ios && pod install)
Lycopodium answered 25/2 at 1:29 Comment(0)
A
0

Permissions for gallery, camera, and microphone (etc) should be added to the ios/Runner/info.plist file, just before the UILaunchStoryboardName key.

<dict>
    <!-- permissions -->
    <key>NSPhotoLibraryUsageDescription</key>
    <string>Please grant photo gallery access</string>
    <key>NSCameraUsageDescription</key>
    <string>Please grant camera access</string>
    <key>NSMicrophoneUsageDescription</key>
    <string>Please grant microphone access</string>
    <!-- end permissions -->
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
</dict>

Note that a plist file is tab-delimited, so you should not use spaces.

Adolfoadolph answered 15/2, 2022 at 3:52 Comment(0)
H
0

you should try by updating the podfile

You can follow this link

By this you can set the permission by default and on the permission dialog, it'll update the permission

Hophead answered 31/8, 2023 at 13:42 Comment(2)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewBristow
@Bristow thanks for the clarification you can add the permissions directly in the podfile Example: post_install do |installer| installer.pods_project.targets.each do |target| flutter_additional_ios_build_settings(target) target.build_configurations.each do |config| config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [ '$(inherited)', ## dart: PermissionGroup.camera 'PERMISSION_CAMERA=1', ## dart: PermissionGroup.microphone 'PERMISSION_MICROPHONE=1' ] end end endHophead

© 2022 - 2024 — McMap. All rights reserved.