difference between react-native link and cocoapods

2020-07-10 10:54发布

问题:

What does react-native link do?

https://github.com/react-native-community/react-native-svg doc says to do

react-native link react-native-svg

is it the same as

pod 'RNSVG', :path => '../node_modules/react-native-svg'  
# pod install 

回答1:

Cocoapods is the dependency manager for iOS. Just like npm is for JavaScript (more specifically Node.js) projects.

Let's switch the example to use react-native-device-info.

The project asks for you to create or add the following to a Podfile (if you're linking manually).

pod 'RNDeviceInfo', :path => '../node_modules/react-native-device-info'

This will add RNDeviceInfo as an iOS dependency and will look inside of '../node_modules/react-native-device-info' to get it. More specifically it'll look for the .podspec file.

If you take a look at the .podspec file, you'll see something like

s.source_files = "RNDeviceInfo/*.{h,m}". All the file is going to do is grab the matching sources (RNDeviceInfo.h and RNDeviceInfo.m) and store them inside of RNDeviceInfo directory. I think (total guess), the directory name matches s.name.

react-native link ... effectively does the same thing, automatically. I quote from React docs:

Link your native dependencies: react-native link Done! All libraries with native dependencies should be successfully linked to your iOS/Android project.

Both accomplish the same result, using react-native link will automate the linking of native libraries.



回答2:

Just like this answer said, react-native link add your dependencies in cocoapods Podfile for you automatically, if your ios project used cocoapods.

But it is not like this before react-native v0.50.0. It wrote dependencies' info ( file reference, header search path, library) into your project's xcodeproj directly. You still can see the old code in react-native v0.57.

So, why uses cocoapods?

I guess it's for consistent package management. Without cocoapods, if your dependency, named A, is dependent on another popular module B from iOS communtity , the author of A would need to write some script like install-B.sh to download B's files, or even worse add B into A project directly.

See ios-install-third-party.sh in react-native v0.48 for example.

Also, your iOS development colleagues would like to use cocoapods(rather than xcodeproj). It is important to use the same package manager for javascript developers and iOS developers. Both you know all native modules that your project includes.

react-native link with cocoapods can let we leverage the two communities.