Adding iOS Permissions with Expo Config Plugins
Introduction
This guide explains how to add various iOS permissions to your Expo project using Config Plugins. By following these instructions, you can customize your app's Info.plist file to request necessary permissions from users.
Initial Setup
You could follow the steps provided in the Bare App Config for a comprehensive guide to set up your project with Expo Config Plugins as the below steps only cover a small part of the process.
Within your theme, create a new file named app.plugin.js
at its root with the following structure:
const { withInfoPlist } = require('@expo/config-plugins');
const withIosPermissions = (config) => {
return withInfoPlist(config, (config) => {
// Permission keys will be added here
return config;
});
};
module.exports = withIosPermissions;
Common iOS Permissions
Here are some common iOS permissions and their corresponding Info.plist
keys:
- Photo Library:
NSPhotoLibraryUsageDescription
- Camera:
NSCameraUsageDescription
- Microphone:
NSMicrophoneUsageDescription
- Location (When In Use):
NSLocationWhenInUseUsageDescription
- Location (Always):
NSLocationAlwaysUsageDescription
- Contacts:
NSContactsUsageDescription
- Calendar:
NSCalendarsUsageDescription
- Reminders:
NSRemindersUsageDescription
- Motion & Fitness:
NSMotionUsageDescription
Using the Config Plugin
To add permissions, modify the withIosPermissions
function. For example, to add Photo Library and Camera permissions:
const withIosPermissions = (config) => {
return withInfoPlist(config, (config) => {
config.modResults.NSPhotoLibraryUsageDescription =
'We need access to your photos to let you choose profile pictures and share images.';
config.modResults.NSCameraUsageDescription =
'Camera access is required to take photos for your profile and to scan QR codes.';
return config;
});
};
You could add multiple permissions by adding more keys to the config.modResults
object.
Applying the Config Plugin
To use the Config Plugin in your app, add the following to your package.json:
{
"expo_plugins": [
"@appmaker-packages/your-theme-package-name/app.plugin.js"
]
}
This will add all the specified permissions to your Info.plist when you build your app.
Remember to only include permissions that your app actually needs, as unnecessary permission requests can lead to rejection during the App Store review process.