Search code examples
ioscordovaionic2cocoapodsplugman

Ionic2 integrate custom iOS plugin with Alamofire framework as dependency


I have installed on my machine cordova 7.0 & Ionic 3.4.0. I made a custom plugin using plugman and it's structured as following:

src - ios & android folders with the native files
www - .js file
plugin.xml
package.json

Adding it to a Ionic 2 project (which is using cordova-ios 4.4.0) works fine, it's generated as expected and I can use it.

Now I need to add Alamofire framework as a dependency to my plugin. I need it to be handled automatically when a plugin is installed. I saw from the documentation (How to add framework as dependency) that I should add:

<framework src="GoogleCloudMessaging" type="podspec" spec="~> 1.2.0" />

To the plugin.xml in the custom plugin. I added it but when I re-add the plugin into the ionic2 project it is not installing also the framework/pod.

I saw other topics related to mine but neither is helping me. I've tried everything, but I can't get it done.

Also, something weird with the tag from the plugin.xml, according to the documentation is:

Paired with type="podspec", this is the spec string for the CocoaPod you want to install (static library only). CocoaPod support only exists in cordova-ios 4.3.0 and cordova-cli 6.4.0. For your plugin, make sure you add the appropriate tags and package.json dependencies to ensure backwards-compatible support.

Isn't this quite strange, will it work for newer versions of cordova-ios?

Plugin - package.json content:

{
    "name": "cordova-plugin-requestService",
    "version": "1.0.0",
    "author": "*******",
    "private": true,
    "engines": {
        "cordovaDependencies": {
            "1.0.0":  { "cordova-ios": ">=4.4.0" }
        }
    }
}

plugin.xml content:

<!-- iOS -->
<platform name="ios">
    <dependency id="cordova-plugin-add-swift-support" version="^1.6.0" />
    <framework src="Alamofire" type="podspec" spec="~> 4.4.0" />
    <config-file target="config.xml" parent="/*">
        <feature name="requestService">
            <param name="ios-package" value="requestService" />
        </feature>
    </config-file>
    <source-file src="src/ios/requestService.swift" />
</platform>

Solution

  • I mainly solved the problems:

    • Made a fresh starter of Ionic (with the latest versions of both cordova and ionic) - will migrate to the newer versions because of the incompatibilities between the cli's and project.

    • Used tag in the plugin.xml of the custom plugin

    • Added platform - without the plugin (for now)
    • Manually add the plugin (it will generate the podfile into the platform and other things) It will although throw an error because of the Alamofire. Add to the podfile use_frameworks! and it should be fine.

    I also added the plugin automatically while triggering platform add ios

    Unfortunately, cordova doesn't support yet this, but it's an open issue: https://issues.apache.org/jira/browse/CB-11893

    You can use either a hook after add platform or https://github.com/blakgeek/cordova-plugin-cocoapods-support if you don't want to write it everytime, manually.