Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add react-native setup-ios-permissions command #750

Merged
merged 10 commits into from Feb 26, 2023

Conversation

zoontek
Copy link
Owner

@zoontek zoontek commented Feb 15, 2023

Hi folks 👋

Let's face it: the current iOS permission handlers setup is not that great. Having to deal with the Podfile file is a chore and not well understood by "non-native" developers.
This pull request adds a new system based on a new react-native CLI command: setup-ios-permissions. To use it, it's fairly easy!

1️⃣ First declare the used iOS permissions in your reactNativePermissionsIOS config (the command uses cosmiconfig, so we can put that in our project package.json if we want):

{
  "reactNativePermissionsIOS": [
    "Camera",
    "LocationWhenInUse",
    "Notifications",
    "SpeechRecognition"
  ]
}

2️⃣ Then run the CLI command:

$ npx react-native setup-ios-permissions

3️⃣ Don't forget to reinstall Pods:

$ npx pod-install

4️⃣ And…it's done ✨

P.-S. Note that you can use a postinstall script to simplify this:

{
  "scripts": {
    "postinstall": "react-native setup-ios-permissions && pod-install"
  },
  "devDependencies": {
    "pod-install": "0.1.38"
  },
  "reactNativePermissionsIOS": [
    "Camera",
    "LocationWhenInUse",
    "Notifications",
    "SpeechRecognition"
  ]
}

P.-S. (2) Did I mention that this approach fixes the issue with use_frameworks! that need this ugly workaround? 👀 (As permissions handlers will now be parts of the RNPermissions pod directly):

pods

@mikehardy
Copy link

Fascinating!! Very nice (as probably the creator of all the motivation for use_frameworks adoption in react-native 😅 )

@thymikee
Copy link
Contributor

thymikee commented Feb 17, 2023

I wonder about the name in package.json. "iosPermissions" is pretty generic and doesn't immediately point to this library. Since this is probably gonna be copy-pasted initially, how about something more explicit like "reactNativePermissionsIOS"? On the other hand, what's the chance other nodejs tool would use this namespace? Probably really low. Thoughts inspired by react-native-community/discussions-and-proposals#588

@zoontek
Copy link
Owner Author

zoontek commented Feb 17, 2023

@thymikee Good though! I updated the config key.

@zoontek zoontek force-pushed the add-setup-ios-permissions-cmd branch from 35a61ff to ffebb54 Compare February 26, 2023 10:50
@zoontek zoontek marked this pull request as ready for review February 26, 2023 11:31
@zoontek zoontek merged commit 8e7c47d into master Feb 26, 2023
@zoontek zoontek deleted the add-setup-ios-permissions-cmd branch February 26, 2023 11:32
zoontek added a commit that referenced this pull request Feb 26, 2023
* Add setup-ios-permissions command

* Improve wording

* Update description

* Remove warning emoji

* Update config key

* Add support for .mm files (new architecture)

* Remove mention of the workaround

* Update documentation

* Remove cache mention

* Bump version number
@helenaford
Copy link

helenaford commented Aug 9, 2023

@zoontek I appreciate the library and all the work you put in 🙏 just struggling a little to see how this makes it easier or more performant. Sometimes you just want to run yarn install to update the js node modules without running pod install everytime.

For some projects im working on, they use expo, so this is a very common scenario where pods aren't needed when running yarn install .

Any way to have a solution where the pods are nicely structured but without the need for an setup script? (cc @mikehardy ;) )

@zoontek
Copy link
Owner Author

zoontek commented Aug 9, 2023

@helenaford You actually don't have to run pod install each time you run a yarn install. I added that in README so people don't forget it.

You can remove the pod install from postinstall if you want, just don't forget to run it manually every time you change the reactNativePermissionsIOS config / every time you delete the node_modules directory or run yarn install --force

@helenaford
Copy link

@zoontek yeah sorry, that's what i meant, every time the node modules are removed, you have to run the setup script right?

So for CI, we need the postinstall step?

@helenaford
Copy link

also, for local development, i'd say it's not great to have that manual step. I can see how that can easily get missed out each time the node modules are cleared

@zoontek
Copy link
Owner Author

zoontek commented Aug 9, 2023

@helenaford Yes. The CLI modify the podspec source paths, then you run the pod install.

Not ideal, but unfortunately the best compromise we found yet 😕

@helenaford
Copy link

helenaford commented Aug 10, 2023

@zoontek like i said above, i really appreciate how hard this is to do and what the intention is to try and make it as easy as possible, but in my opinion, the other way was better because you just configured it once per permission module. Rather than everytime the node modules are cleared.

is there a way to have two methods to setup the library?

@zoontek
Copy link
Owner Author

zoontek commented Aug 10, 2023

@helenaford The other way is still possible (the podspecs per handler are still there), but will probably be removed in next major version as it creates way too much issues with Xcode caching.

Maybe we could add a similar command for the Pod file? Something like use_react_native_permissions!, able to read the reactNativePermissionsIOS too? I will investigate.

@helenaford
Copy link

@zoontek thank you that would be perfect if we could put something in the Podfile. thanks for looking into this! 😎

@zoontek
Copy link
Owner Author

zoontek commented Aug 11, 2023

@helenaford Could you try it? I released a new version, 3.9.0-beta.0 with it.

yarn add react-native-permissions@next

Then update your Podfile like this:

require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'
+ require_relative '../node_modules/react-native-permissions/scripts/permissions_setup'

platform :ios, min_ios_version_supported
prepare_react_native_project!
+ prepare_react_native_permissions!

And run pod install (pod install must be re-executed each time you update reactNativePermissionsIOS config, but that's all)

PS: Don't forget to remove the pod-install call on postinstall if you want, as it's not needed anymore.

If you are curious, this is the script: https://github.com/zoontek/react-native-permissions/blob/add-ruby-setup-script/scripts/permissions_setup.rb

Basically a conversion of the react native CLI plugin to ruby. Thanks again for the insight!


I'm not sure about the wording (the script file + function name) tho, it is subject to change before a release. Does it sounds good for you? @mikehardy @thymikee, do you have an opinion / some ideas for that?

Note that putting everything in the Podfile (no reactNativePermissionsIOS config anymore), could also be a solution. It might even be simpler:

require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'
+ require_relative '../node_modules/react-native-permissions/scripts/permissions_setup'

platform :ios, min_ios_version_supported
prepare_react_native_project!

+ setup_permissions([
+  'Camera',
+  'LocationWhenInUse',
+  'Microphone'
+ ])

@zoontek
Copy link
Owner Author

zoontek commented Aug 11, 2023

I added a commit with it (could be rolled back): 51d97fa. This looks neat, if you ask me 😄. And as the user will have to update his Podfile to add / delete a permission, having to run a pod install will make total sense for him.

EDIT: I published it as 3.9.0-beta.1, as I find this solution much more elegant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants