How to bundle CocoaPods dependencies with Cordova Plugin?
Asked Answered
D

5

33

I'm developing a Cordova plugin, and for the iOS platform, I'd like to incorporate a few existing (non-cordova-specific) Cocoapods. I haven't been able to find any info or examples on how to get this working, and I would've expected this to be a fairly common use-case.

Note that I am not trying to use Cordova as a Cocoapod, for which there is a decent amount of information out there, but rather use Cocoapods from within the iOS platform of a Cordova plugin that I'm developing.

Cordova's plugin.xml generally needs to list all project dependencies to bundle things correctly. Does anyone know of a Cordova or PhoneGap plugin which internally uses Cocoapods?

Dorolice answered 20/8, 2014 at 21:4 Comment(3)
Well, if this really is an unsolved problem, the Cordova / PhoneGap team really really needs to address it, given that being able to take advantage of widely used CocoaPods from within native plugins would significantly boost the amount and quality of plugins out there (iOS first in this case).Dorolice
did you make any progress with this?Hackamore
No; I haven't been able to find any help and don't understand the Cocoapod / cordova build processes enough to spend the time implementing it myself.Dorolice
H
39

I just wasted several hours on this so I thought I would put this here for anyone who is interested. Cordova as of Cordova-CLI version 6.4.0/Cordova iOS version 4.3.0 supports direct integration with CocoaPods.

Detailed information is here: https://cordova.apache.org/docs/en/latest/plugin_ref/spec.html

Simply add something like:

<platform name="ios">
    ...Other iOS platform specific declerations...
    <framework src="OpenSSL" type="podspec" spec="~> 1.0.0" />
</platform>

To the plugin.xml file for your plugin. Cordova automatically generates an .xcworkspace. Use that instead of the xcodeproj file and you are good to go.

Harve answered 11/2, 2017 at 19:54 Comment(7)
As of 2/21/2017 this appears to now be the best answer. By adding a podspec entry to the plugin.xml file, it creates the file "platform/ios/Podfile" on the MAC agent when compiling a Cordova project using Visual Studio. I verified that the dependency was downloaded and successfully included in the compile.Onepiece
THANK YOU! maybe this is obvious to iDevs who work with xcode and pods on the daily.... but I would have never figured this out.... that we should use the workspace not the project.Cattleya
@Onepiece I'm having trouble to add a pod to my custom plugin. Cordova docs say the pod must be a static library only. Is it possible to bundle a pod written in swift to a plugin in a cordova project ?Mariande
This doesn't seem to support podspecs with embedded frameworks, nor podspecs that have source files. @blakgeek's cocoapod cordova plugin is a far more flexible solution that supports these scenarios.Chukchi
what if I want to add some git repos as this podspec, how should I do that?Credit
@Onepiece Adding the above line in the plugin.xml creates a "podfile" with the corresponding pod. To install the pod I have to go to "platform/ios/Podfile" and do the pod install. Is that understanding correct ?Fayalite
@Jarra Mclntyre, I found lot of files named plugin.xml. In which file I need to add your code. Found some in cordova-white-list folder, I think this is not the right one.Fierro
C
7

I couldn't find a good solution to this problem so I created a plugin for it, https://github.com/blakgeek/cordova-plugin-cocoapod-support. It adds support for defining Cocoapod dependencies in the plugin.xml. It should be included as a dependency of another plugin and then that plugin can add entries in the ios platform section of the plugin.xml. It supports all of the pod dependency specifications like git repo, configurations, subspecs, etc. Hope this helps someone out.

Cotopaxi answered 16/7, 2016 at 19:0 Comment(1)
This solution supports embedded frameworks, and pods with source code files. This solution seems far superior to what is included with cordova.Chukchi
N
5

I couldn't find much info about this either, so I wrote a Cocoapods plugin to help: https://github.com/xdissent/cocoapods-cordova It manages your plugin.xml automatically and builds a universal static library which links in only non-cordova dependencies. Currently a WIP, but I'm using it successfully on a few plugins.

Nightwear answered 15/9, 2014 at 17:54 Comment(0)
S
2

In recent versions of Cordova, it's recommended to use podspec tag instead of framework tag with type podspec. Here is an example configuration:

<platform name="ios">
    ...
    <podspec>
        <config>
            <source url="https://github.com/CocoaPods/Specs.git"/>
        </config>
        <pods use-frameworks="true">
            <pod name="SwiftyJSON" spec="~> 5.0.0" />
        </pods>
    </podspec>
    ...
</platform>
Subhead answered 25/1, 2021 at 16:41 Comment(2)
Note that config tag with source sub-tag is mandatory otherwise it will raise an error while installing the pluginSubhead
Hi, could you please help me for below queries - Where to add these line? plugin.xml? Do I need to create a plugin.xml in one of the folder? - How to get the pod installed after adding these lines ? Any help would be appreciated :)Osteophyte
P
0

I am meeting a similiar situtation.

Here are two workarounds.

  1. Integrate Cordova into the plugin development environment, i.e, turn a pure Xcode project into a Cordova based project. If we want to use cordova plugins for iOS, they are available via Cocoapods too.

  2. Introduce Cocoapods to the Xcode project generated by Cordova. This way, we need to put the generated code into source control and be careful when we add other plugins since files we modified can be modified by cordova.

I don't know yet which one is a better choice.

The following pods info are helpful for workaround #1.

pod search cordova

-> Cordova (4.0.1) Cordova iOS pod 'Cordova', '~> 4.0.1' - Homepage: https://github.com/apache/cordova-ios - Source: https://github.com/apache/cordova-ios.git - Versions: 4.0.1, 3.9.1, 3.8.0, 3.7.0, 3.6.0, 3.5.0, 3.4.1, 3.4.0, 3.3.0, 3.2.0, 3.1.0, 3.0.0, 2.9.0, 2.8.0, 2.7.0, 2.6.0, 2.5.0, 2.4.0, 2.3.0, 2.2.0, 2.1.0, 2.0.0, 1.9.0, 1.8.1, 1.8.0, 1.7.0, 1.6.1, 1.6.0, 1.5.0, 1.4.1, 1.4.0, 1.3.0, 1.2.0, 1.1.0, 1.0.0 [master repo]

-> Cordova-Lib (3.9.1) 镜像自github,部署在git.oschina.net解决国内访问的连接速度。Apache Cordova is a platform for building native mobile applications using HTML, CSS and JavaScript. pod 'Cordova-Lib', '~> 3.9.1' - Homepage: https://git.oschina.net/liubiqu/cordova-ios - Source: https://git.oschina.net/liubiqu/cordova-ios.git - Versions: 3.9.1, 3.9.0, 3.8.0 [master repo]

Papua answered 6/6, 2016 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.