flutterflutter-pluginpodspecpodfileflutter-ios

How to manually add a .xcframework to a Flutter iOS Plugin?


I'm trying to create a Flutter Plugin to use a native library. This library I'm trying to use is stored in a private repository and can be used with Swift Dependency Manager.

This is causing me a headache, cause I can't add a private repository dependency in my plugin (I couldn't find a way to do this in .podspec file), so what I've done:

  1. I've added the plugin to the Example project with Swift Package Manager
  2. Manually copied MyDependency.xcframework folder to MyPlugin/ios folder
  3. Referenced it in podspec file, like this:
s.preserve_paths = 'MyDependency.xcframework'
s.xcconfig = { 'OTHER_LDFLAGS' => '-framework MyDependency' }
s.vendored_frameworks = 'MyDependency.xcframework'

Doing this I'm able to use MyDependency inside plugin's sources.

My current problem is: This is only working in Simulator.

Before doing this, the project was running without any problem in real devices.

This is the error message I'm receiving every time I tried to run in a real device: enter image description here

Also, I've made a test using the dependency directly from Swift Dependency Manager and is working fine. I think the problem is the way I'm adding the framework to my plugin.

enter image description here


Solution

  • After doing some research, I've found some links giving me an ideia about the real problem...

    To solve this, I've added a simple script to my main project's build process.

    This script adds the code signing to inner .framework files.

    cd "${CODESIGNING_FOLDER_PATH}/Frameworks/"
    
    # flatten nested frameworks by copying to APP.app/Frameworks
    for framework in *; do
        if [ -d "$framework" ]; then
            if [ -d "${framework}/Frameworks" ]; then
                echo "Moving embedded frameworks from ${framework} to ${PRODUCT_NAME}.app/Frameworks"
                cp -R "${framework}/Frameworks/" .
                rm -rf "${framework}/Frameworks"
            fi
        fi
    done
    
    # remove any leftover nested frameworks (i.e. 'PackageName_359AFEED79E48935_PackageProduct.framework')
    for framework in *; do
        if [ -d "$framework" ]; then
            if [ -d "${framework}/Frameworks" ]; then
                echo "Removing embedded frameworks from ${framework} to ${PRODUCT_NAME}.app/Frameworks"
                rm -rf "${framework}/Frameworks"
            fi
        fi
    done
    
    # codesign for Debugging on device
    if [ "${CONFIGURATION}" == "Debug" ] & [ "${SDKROOT}" != *Simulator* ] ; then
    
        echo "Code signing frameworks..."
        find "${CODESIGNING_FOLDER_PATH}/Frameworks" -maxdepth 1 -name '*.framework' -print0 | while read -d $'\0' framework
        do
            # only sign frameworks without a signature
            if ! codesign -v "${framework}"; then
                codesign --force --sign "${EXPANDED_CODE_SIGN_IDENTITY}" --preserve-metadata=identifier,entitlements --timestamp=none "${framework}"
                echo "Added missing signature to '${framework}'"
            fi
        done
    fi