React Native using Cocoapods - ios

Is it possible to use cocoapods when using React Native?
If so, How can I require in JavaScript the pods projects?
Thanks in advance!

Yes. It's possible. React Native project is pretty much normal iOS application (In terms of XCode required to build it and quite a lot of React Native internal code is well... Native Obj-C). Since React Native project is already run via xcworkspace, so it's perfectly possible to add Obj-C pods to the ReactNative project.
Since a lot of dependencies used by RN apps are pure javascript rather than Obj-C code, it's more than convenient to keep both dependency systems - Cocoapods (for Obj-C dependencies) and npm (for javascript). Pods are kept in "Pods" directory and npm under "node-modules" and they are not clashing with each other. And it's pretty convenient actually to have different types of dependencies run by different dependency management systems.
UPDATE: by default ReactNative project is .xcodeproj based, but it's easy to convert it to .xcodeworkspace.
I even tried to add React Native itself as Cocoapods dependency and it was sort of working (but some dependencies expected React in the "node-modules" dir so I abandoned it).

Related

Upgrading React Native plugin to support autolinking and React Native versions 0.6>=

I am fairly new to React Native but am helping manage a React Native plugin and would like some guidance for upgrading it so that it is compatible with auto linking and React Native versions 0.60 and up in iOS.
Our plugin currently only works with React Native up to and including 0.59.x through the react-native link command and sometimes involves manually dragging our xcodeproj and .a binary inside Xcode.
When trying to instrument our plugin with a blank hello world React Native app of version 0.60.5, after react-native link and dragging in Xcode, we get an error saying rctbridgemodule.h not found and have to add React.xcodeproj too. This however doesn’t work on some apps we’ve tried. Furthermore the issue is in newer versions of React eg 0.61.4, the React.xcodeproj file is no longer in the React folder in node_modules. 

I’ve had a look at the What do I need to have in my package to make it work? Section in this link: https://github.com/react-native-community/cli/blob/master/docs/autolinking.md
But to me its not very clear how we should be configuring a pod spec in the root of our repo. Our package is scoped also. I’m assuming that there shouldn’t need to be any changes to the actual code of our plugin but rather just some modifications to our pod spec and package configuration. Any guidance in how this works and how to upgrade our plugin to support versions 0.6 and above would be greatly appreciated. Thanks!

Upgrade JS only react-native library to RN 0.60

I have a react-native component library, which only contains javascript code. I don't have ios/android directory (in library). I want to make it compatible with react-native 0.60. My library has some dependencies which which needs linking. Until this point I was just asking consumers to link these dependencies manually.
Now for autolinking (in RN 0.60) to work, I will need to a podspec for my project. But I don't really have any ios code (or directory) in my library. So I can't create podspec. So in this case, only way I can see is to ask consumers of my library to manually install all the NPM dependencies of my libraries and then pod install. Is there some other way that I can configure it in my library, that will let cocoapods know to install the dependencies of my library as well?
P.S. I am not really much familiar with ios or cocoapods. So if this question sounds a bit silly, please pardon me.
You can try to run following command to generate ios/android directories:
react-native upgrade --legacy true
Then you can run following command to install iOS dependencies when the directories generated:
cd ios
pod install
I don't think this is possible at least with version 0.60, Because of breaking changes in this version, If you are on the version before 0.60 and wanted to upgrade on 0.60 you have to face native issues and breaking changes in the native libraries as well because for now most of the libraries have two different versions one for above 0.60 and other for below 0.60.
When you will try to upgrade on 0.60 this version also have native changes and also auto linking is been introduced so some native libraries can also cause problem due to this.
My Recommendation is to use Upgrade helper tool and get an idea about the changes that you need on every version.
https://facebook.github.io/react-native/docs/upgrading

Duplicate Symbol Problem With React Native

So I've been facing a really weird problem with React Native.
I installed a library via Cocoapods for picking images from the camera roll/photo Library and cropping the image as well, it is a silver bullet for this kind of usage. But then, it installs alongside React Native version 11, but this version of React Native was not what I wanted so I had to install React Native via Cocoapods as well so I can have the React Native version I'm using.
Now all installed well and if I try to build in my Dev environment everything works fine but when I try to archive, I get the error for duplicate symbols for WebSocket, Image (the library requires you to add RCTImage as a subspec in Cocoapods for React Native), RCTText, etc. basically all the subspecs you're required to add if installing React Native via Cocoapods according to the React Native docs.
So I figured I'll need to remove the manually linked libraries from my project, so I did that, but that only resulted in me now having another error when building for dev saying WebSocket, Linking, Network, etc are missing. Basically all the subspecs you're adding via Cocoapods, so I don't understand, if I install React Native and all its subspecs via Cocoapods (I'm also running in the workspace) shouldn't my project use the installed pods and not the linked libraries?
I've been on this for like 3 weeks now.
Here is my pods list.
Here is my linked binaries list.
Please, what do I do? I've been stuck for 3 weeks now, have tried almost any solution online but nothing has changed.
Thanks in advance.
I have solved the problem, only thing is I think you should use this carefully because I don't fully understand what it does. So within the docs, there is this Cocoapods code snippet that when you add to the Pods file after your target ends resolves this problem. Here is the code snippet.
# very important to have, unless you removed React dependencies for Libraries
# and you rely on Cocoapods to manage it
post_install do |installer|
installer.pods_project.targets.each do |target|
if target.name == "React"
target.remove_from_project
end
end
end
If you know exactly what this does please post another answer with this code snippet and an explanation so I can make the answer to the question, or better yet, edit the answer with the explanation, or leave a comment below so I can update my answer. From the looks of the above code snippet, though I'd say this is removing React and it's Subspecs from the installed pods, right after installation, I might be wrong.
Now when you're done with the installation, you'll get another error when you try to build saying react-native or React could not be resolved because it is in multiple locations and that you should delete one of them, to resolve this new error, simply go to the Pods directory and delete the React folder, that is [project-root]/ios/Pods/React once you do this, your application will build for development and also archive for production.
This problem I faced stands as a result of not properly reading installation guides, there may be small discrepancies between how it's usually done and how it's done for the library you're installing, so try and give a little more attention to detail.
For me,
delete all Link Binary With Libraries (From Build Phase)
delete Podfile.lock
run pod install
Wait for Indexing In Xcode
command+shift+k (Clear the project)
build again and fix it.

How to use the Framework with external dependency

I'm having the framework which has the external dependency with Alamofire. If I try to use the framework in my project, it showing ld: framework not found Alamofire in framework
I had setup all things to consume the framework in project.
Added framework in Embedded binaries and Build copy phase file
Things which I have tried: Check here
I need to use my framework with external pod dependencies.
Note: I have searched a lot and tried many solutions but know work in my case.
You may use a dependency manager like Carthage or CocoaPods in order to manage external dependencies in your framework. Follow their instructions how to use a framework from within a framework. This is the easy part.
Enable CocoaPods respectively Carthage in your framework so that it can be imported as external dependency by other projects. For CocoaPods this is more elaborated and requires you to create and publish a Podspec. Note, that you can create a private Pod if you don't want to share your framework. You will find more resources in the web (CocoaPods) how to accomplish all this. For Carthage usually there's little to no extra effort. Host your framework in some repository (private or public).
In your application, follow the instructions from the dependency manager how to import dependencies for an app. You possibly need to explicitly add secondary dependencies for Carthage (i.e. Alamofire) much like you add other depenendcies. CocoaPods would do this for your app project automatically when you install the Pods.
Import your framework in your sources. Before you build, ensure your dependencies will be build. Again, look for the documentation of the dependency manager how you accomplish this.

Standalone framework

I'm working on a project where I created a SDK (cocoapods) with two dependencies - AFNetworking and Realm.
Here is my process:
I create cocoapods project (in this project code is visible)
I use cocoapods-package to build a framework (with mangled symbols).
I then copy this framework to another cocoapods project which will be distributed to the public.
User who wants to use this SDK then 'installs' this cocoapod. When he does this AFNetworking and Realm are installed alongside (because they are dependencies)
What I would like to do is somehow embed AFNetworking and Realm inside a framework. I know that framework will be bigger in size. Main reason for doing this is so that user is not obligated to use the same version of AFNetworking and Realm.
The CocoaPods Packager is taking care of what you want here automatically.
You can pull in third-party dependencies using CocoaPods. (CocoaPods Packager is even capable of mangling symbols to improve compatibility with any symbols that might appear in the integrating app.)
The packager builds open-source podspecs for you. This podspec needs to declare all sources, resources and dependencies. Please note that podspecs never lint nor build when not all of their dependencies are declared correctly.
But as the packager takes care of vendoring the dependencies by mangling their symbols, it allows you to create a big binary, where the dependencies were included from static libraries.
On base of that build product, you can have a modified version of your podspec. This should not declare the dependencies anymore when they are included and shouldn't need declare the source files or need to have them publicly available anywhere, but instead you declare the built binary as vendored library. This still needs to specify the header files and have them accessible, so that consumers of the podspec are able to interface the API of your library.
The CocoaPods Packager creates this variant of the podspec for you as well.

Resources