Embedding a framework within a framework (iOS 8+)

2020-02-04 05:47发布

问题:

iOS applications built with Xcode 6 or higher allow embedding dynamic iOS frameworks within them. I am building a shared framework and would like to embed a sub-framework. How can I accomplish this?

Note: This is possible and is being used in production (for e.g., with Swift frameworks in CocoaPods).

回答1:

Found the answer. Here's how it's done:

  • Navigate to Target > Build Phases
  • Click the small "+" icon and select "New Run Script Build Phase"
  • Paste the following:

    cd $BUILT_PRODUCTS_DIR
    
    mkdir $PROJECT_NAME.framework/Frameworks &>/dev/null
    
    for framework in *.framework; do
        if [ $framework != $PROJECT_NAME.framework ]; then
            cp -r $framework $PROJECT_NAME.framework/Frameworks/ &>/dev/null
        fi
    done
    


回答2:

@Vatsal Manot's answer was very helpful for me. I modified it a bit and also had a need to sign the copied embedded framework. My script is below.

cd $BUILT_PRODUCTS_DIR/$PRODUCT_NAME.app/Frameworks/Custom.framework/Frameworks
for framework in *.framework; do
    mv $framework $BUILT_PRODUCTS_DIR/$PRODUCT_NAME.app/Frameworks/
    /usr/bin/codesign --force --sign "iPhone Developer" --preserve-metadata=identifier,entitlements --timestamp=none $BUILT_PRODUCTS_DIR/$PRODUCT_NAME.app/Frameworks/$framework
done


回答3:

To create a Umbrella Framework that contains a Sub-Framework you can follow the step-by-step guide written down here: Umbrella framework