Search code examples
iosobjective-ccordovaionic-frameworkcordova-plugins

How to properly reference an iOS 3rd party framework in Ionic/Cordova build so it doesn't throw dyld: Library not loaded: @rpath error


I have an Ionic5 (cli 6.3.0)/Cordova 9.0.3 ([email protected]) project that uses plugins.

I am attempting to create a custom plugin for a 3rd party framework.

I know the skeleton of the plugin is correct since I created it with Plugman and before adding the framework I could build/run and get feedback that "coolMethod" had been run from Objective-C file.

Even after adding the framework, I can get the plugin to install correctly with ionic cordova plugin add ./packages/cordova-plugin-my-custom-stuff

I can get Ionic/Cordova to build the iOS project successfully with ionic cordova build ios

But when I try to run the iOS emulator from either Ionic command line ionic cordova run ios or from auto-generated xCode project, I get

 dyld: Library not loaded: @rpath/MyThirdPartySDK.framework/MyThirdPartySDK
 Referenced from: /Users/myUser/Library/Developer/CoreSimulator/Devices/C9E28EFF-B017-4F5E- 
 BA89-5200AE5D64EA/data/Containers/Bundle/Application/775C8394-B014-4B01-89CA- 
 1D462FD54F4A/MyName Mobile-UAT.app/MyName Mobile-UAT
 Reason: image not found

The structure of the project (condensed for clarity) is like so:

 MyProject
 -node_modules
 -packages
  -cordova-plugin-my-custom-stuff
   -src
    -ios
     MyCustomStuff.m
     -MyThirdPartySDK.framework
      -Headers
       MobileAPI.h
       MyThirdPartySDK.h
      -Modules
       module.modulemap
      MyThirdPartySDK
   -www
    cordova-plugin-my-custom-stuff.js
   package.json
   plugin.xml

The plugin.xml ios section looks like:

<platform name="ios">
    <config-file parent="/*" target="config.xml">
        <feature name="MyCustomStuff">
            <param name="ios-package" value="MyCustomStuff"/>
        </feature>
    </config-file>
    <source-file src="src/ios/MyCustomStuff.m"/>
    <source-file src="src/ios/MyCustomStuffSDK.framework" framework="true"/>
    <header-file src="src/ios/MyCustomStuffSDK.framework/Headers/MobileAPI.h" target-dir="MyCustomStuff" />
</platform>

I am looking for a solution that resides within the plugin.xml configuration or some other ionic /cordovaproject configuration. I would like to avoid editing the xCode project as this is all generated "automagically" by the ionic/cordova command line.


Solution

  • Finally figured it out.

      <platform name="ios">
        <config-file parent="/*" target="config.xml">
            <feature name="MyCustomPlugin">
                <param name="ios-package" value="MyCustomPlugin"/>
            </feature>
        </config-file>
        <source-file src="src/ios/MyCustomPlugin.m"/>
        <source-file src="src/ios/MyCustomPlugin.framework" target-dir="lib" framework="true" />
        <framework src="src/ios/MyCustomPlugin.framework" embed="true" custom="true" />
        <header-file src="src/ios/MyCustomPlugin.framework/Headers/MobileAPI.h" target-dir="MyCustomPlugin" />
     </platform>