zoontek / react-native-permissions

An unified permissions API for React Native on iOS, Android and Windows.
MIT License
4.04k stars 828 forks source link

Confusion with setup instructions and Expo #881

Closed ajnozari closed 3 months ago

ajnozari commented 3 months ago

Before submitting a new issue

Bug summary

I'm trying to get this working on the latest version of expo (fresh install) but I'm confused on something. If I'm using expo development builds do I need to do the pod file setup? I only ask as I noticed that any changes are reset when running yarn expo prebuild --clean.

I apologize if this question has already been answered but I feel the documentation could be made a little clearer on what is required for Expo users. Currently there is the section on the plugin, but I'm not sure if that also handles calling setup_permissions, as I am getting a No permission handler detected issue but even when I try to add the relevant sections based on your example repo I cannot seem to get it to work.

I appreciate any assistance.

Library version

4.1.5

Environment info

System:
  OS: macOS 14.3
  CPU: (10) arm64 Apple M1 Max
  Memory: 78.16 MB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 22.2.0
    path: /opt/homebrew/bin/node
  Yarn:
    version: 4.0.2
    path: /opt/homebrew/bin/yarn
  npm:
    version: 10.7.0
    path: /opt/homebrew/bin/npm
  Watchman:
    version: 2024.05.06.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.15.2
    path: /opt/homebrew/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.5
      - iOS 17.5
      - macOS 14.5
      - tvOS 17.5
      - visionOS 1.2
      - watchOS 10.5
  Android SDK:
    API Levels:
      - "29"
      - "30"
      - "31"
      - "32"
      - "33"
      - "33"
      - "33"
      - "34"
    Build Tools:
      - 28.0.3
      - 29.0.2
      - 29.0.3
      - 30.0.2
      - 30.0.3
      - 31.0.0
      - 32.0.0
      - 33.0.0
      - 33.0.0
      - 34.0.0
    System Images:
      - android-31 | Google APIs ARM 64 v8a
      - android-33 | Google APIs ARM 64 v8a
      - android-34 | Google APIs ARM 64 v8a
      - android-VanillaIceCream | Google Play ARM 64 v8a
    Android NDK: Not Found
IDEs:
  Android Studio: 2023.3 AI-233.14808.21.2331.11842104
  Xcode:
    version: 15.4/15F31d
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.11
    path: /opt/homebrew/opt/openjdk@17/bin/javac
  Ruby:
    version: 2.6.10
    path: /usr/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.74.2
    wanted: patch:react-native@npm%3A0.74.2#~/.yarn/patches/react-native-npm-0.74.2-03c85e9132.patch
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: false

Steps to reproduce

  1. Create new expo project
  2. Install Repo
  3. run yarn expo prebuild
  4. run yarn expo run:ios

Reproducible sample code

I cannot offer code as it's truly a minimal setup.
zoontek commented 3 months ago

@ajnozari Let's say you want to ask for microphone permission. You will have to update your app.json file this way:

{
  "expo": {
    "name": "Awesome app",
    "plugins": [
      [
        "react-native-permissions",
        {
          // Add setup_permissions to your Podfile (see iOS setup - steps 1, 2 and 3)
          "iosPermissions": [
            "Camera",
            "Microphone"
          ]
        }
      ]
    ],
    "ios": {
      // Add descriptions to your Info.plist (see iOS setup - step 4)
      "infoPlist": {
        "NSCameraUsageDescription": "[REASON]",
        "NSMicrophoneUsageDescription": "[REASON]"
      }
    },
    "android": {
      // Add permissions to your AndroidManifest.xml (see Android setup)
      "permissions": [
        "android.permission.CAMERA",
        "android.permission.RECORD_AUDIO"
      ]
    }
  }
}

I'm updating the README to make this easier to understand.

ajnozari commented 3 months ago

Thank you, once all of the relevant sections were added expo prebuild is now working as expected.

alketii commented 1 month ago

I am not having any luck modifying t he Podfile, I keep getting "No permission handler detected"

There was a problem loading the project.
This development build encountered the following error:
• No permission handler detected.
• Check that you are correctly calling setup_permissions in your Podfile.
• Uninstall this app, reinstall your Pods, delete your Xcode DerivedData folder and rebuild it.
-[RNPermissions constantsToExport]
RNPermissions.mm
-[RCTModuleData gatherConstantsAndSignalJSReq
uire Ending:]
-[RCTModuleData exportedConstants]
facebook::react::RCTNativeModule::getConstants)
facebook::react::ModuleRegistry::getConfig (std::
1:: basic_string<char, std::_1::char_traits<char>, st d::
_1: :allocator<char>> const&)
facebook::react::JSINativeModules::createModulef acebook: ji:: Runtime&, std::_1:: basic_string<char, std::
_1::char_traits<char>, std::_1::allocator<char
>> const&)

I already added the permission to app.json and I am using react-native-clean-project before each build.

This is my Podfile:

# Podfile

require File.join(File.dirname(`node --print "require.resolve('expo/package.json')"`), "scripts/autolinking")
require File.join(File.dirname(`node --print "require.resolve('react-native/package.json')"`), "scripts/react_native_pods")

require 'json'
podfile_properties = JSON.parse(File.read(File.join(__dir__, 'Podfile.properties.json'))) rescue {}

ENV['RCT_NEW_ARCH_ENABLED'] = podfile_properties['newArchEnabled'] == 'true' ? '1' : '0'
ENV['EX_DEV_CLIENT_NETWORK_INSPECTOR'] = podfile_properties['EX_DEV_CLIENT_NETWORK_INSPECTOR']

# Node require function to resolve scripts with node for hoisting
def node_require(script)
  require Pod::Executable.execute_command('node', ['-p', "require.resolve('#{script}', {paths: [process.argv[1]]})", __dir__]).strip
end

# Include React Native and React Native Permissions setup scripts
node_require('react-native/scripts/react_native_pods.rb')
node_require('react-native-permissions/scripts/setup.rb')

platform :ios, podfile_properties['ios.deploymentTarget'] || '13.4'
install! 'cocoapods',
  :deterministic_uuids => false

prepare_react_native_project!

target 'MyApp' do
  use_expo_modules!
  config = use_native_modules!

  use_frameworks! :linkage => podfile_properties['ios.useFrameworks'].to_sym if podfile_properties['ios.useFrameworks']
  use_frameworks! :linkage => ENV['USE_FRAMEWORKS'].to_sym if ENV['USE_FRAMEWORKS']

  use_react_native!(
    :path => config[:reactNativePath],
    :hermes_enabled => podfile_properties['expo.jsEngine'] == nil || podfile_properties['expo.jsEngine'] == 'hermes',
    # An absolute path to your application root.
    :app_path => "#{Pod::Config.instance.installation_root}/..",
    :privacy_file_aggregation_enabled => podfile_properties['apple.privacyManifestAggregationEnabled'] != 'false',
  )

  # Setup permissions with the permissions you need
  setup_permissions([
    'PhotoLibraryAddOnly',
  ])

  post_install do |installer|
    react_native_post_install(
      installer,
      config[:reactNativePath],
      :mac_catalyst_enabled => false,
      :ccache_enabled => podfile_properties['apple.ccacheEnabled'] == 'true',
    )

    # Necessary for Xcode 14, preventing resource bundles from signing on device builds
    installer.target_installation_results.pod_target_installation_results
      .each do |pod_name, target_installation_result|
      target_installation_result.resource_bundle_targets.each do |resource_bundle_target|
        resource_bundle_target.build_configurations.each do |config|
          config.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'
        end
      end
    end
  end

  post_integrate do |installer|
    begin
      expo_patch_react_imports!(installer)
    rescue => e
      Pod::UI.warn e
    end
  end
end
zoontek commented 1 month ago

@alketii Try below prepare_react_native_project!, not inside target 'MyApp' do…end, like in the README / example app.

alketii commented 1 month ago

@zoontek still not working. I also double-checked if I have the other requirements.