美文网首页iOS进阶之路
UmbrellaFramework(一)创建基础framewor

UmbrellaFramework(一)创建基础framewor

作者: 灰豹儿 | 来源:发表于2017-05-24 23:26 被阅读18次

    开发过程中会遇到各种各样的情况,比如需要将几个已经封装好的framework封装成一个,统一提供出去,封装的这种framework就是umbrellaframework。Apple的官方文档中明确提到了不建议自己去创建umbrellaframework,但是既然遇到了这种情况,还是动手试一试。

    首先先引入Apple的 Guidelins for Creating Frameworks 的一段

    Don’t Create Umbrella Frameworks

    While it is possible to create umbrella frameworks using Xcode, doing so is unnecessary for most developers and is not recommended. Apple uses umbrella frameworks to mask some of the interdependencies between libraries in the operating system. In nearly all cases, you should be able to include your code in a single, standard framework bundle. Alternatively, if your code was sufficiently modular, you could create multiple frameworks, but in that case, the dependencies between modules would be minimal or nonexistent and should not warrant the creation of an umbrella for them

    文章将分三个部分逐步创建并使用UmbrellaFramework

    • SubFramework 创建一个基础framework
    • UmbrellaFramework framework里封装framework
    • UmbrellaFrameworkDemo 使用demo

    本篇是第一部分,这一部分将会一步步创建一个基础framework,那么伸伸懒腰,我们开始吧!

    demo地址:https://github.com/huibaoer/Demo_UmbrellaFramework

    创建一个基础的framework

    1.创建一个framework工程:Subframework

    SubFramework_1

    2.添加类SubSayHello,添加sayHello方法

    SubFramework_2

    3.在SubFramework.h头文件中导入SubSayHello.h

    SubFramework_3

    4.将SubSayHello.h添加到公共Header中

    SubFramework_4

    5.Architectures添加armv7s

    SubFramework_5

    6.连接选项 Mach-O Type 选择Static Library静态库

    SubFramework_6

    7.生成通用framework

    到了这一步framework已经基本完成了,只需要生成最终需要使用的framework了。一般有两种方式生成:

    • 分别在模拟器和真机下运行工程,分别导出两个运行出来的framework,用命令行合并成一个通用的framework
    • 在工程中添加脚本,运行脚本生成通用framework

    两种方式介绍的文章很多,这里不做过多解释,下面使用第二种添加脚本的方式生成framework

    7.1为SubFramework工程添加Target -> Aggregate
    SubFramework_7
    7.2在SubFramework新添加的Target中添加脚本

    脚本内容如下:

    # Sets the target folders and the final framework product.
    
    # 如果工程名称和Framework的Target名称不一样的话,要自定义FMKNAME
    
    # 例如: FMK_NAME = "MyFramework"
    
    FMK_NAME=${PROJECT_NAME}
    
    # Install dir will be the final output to the framework.
    
    # The following line create it in the root folder of the current project.
    
    INSTALL_DIR=${SRCROOT}/Products/${FMK_NAME}.framework
    
    # Working dir will be deleted after the framework creation.
    
    WRK_DIR=build
    
    DEVICE_DIR=${WRK_DIR}/Release-iphoneos/${FMK_NAME}.framework
    
    SIMULATOR_DIR=${WRK_DIR}/Release-iphonesimulator/${FMK_NAME}.framework
    
    # -configuration ${CONFIGURATION}
    
    # Clean and Building both architectures.
    
    xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphoneos clean build
    
    xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphonesimulator clean build
    
    # Cleaning the oldest.
    
    if [ -d "${INSTALL_DIR}" ]
    
    then
    
    rm -rf "${INSTALL_DIR}"
    
    fi
    
    mkdir -p "${INSTALL_DIR}"
    
    cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/"
    
    # Uses the Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product.
    
    lipo -create "${DEVICE_DIR}/${FMK_NAME}" "${SIMULATOR_DIR}/${FMK_NAME}" -output "${INSTALL_DIR}/${FMK_NAME}"
    
    rm -r "${WRK_DIR}"
    
    open "${INSTALL_DIR}"
    
    SubFramework_8
    7.3运行新添加的Target,会自动弹出窗口,包含了已经生成好的framework。大功告成!

    8.最后,可以检查一下生成的framework信息,命令行执行如下命令,注意cd到SubFramework的所在目录

    lipo -info  SubFramework.framework/SubFramework
    

    正常情况下结果如下,支持的架构已经显示出来
    Architectures in the fat file: SubFramework.framework/SubFramework are: armv7 armv7s i386 x86_64 arm64

    当然,也可以自己建一个demo工程,将SubFramework导入,试着调用一下 SubSayHello 的 sayHello 方法。相信会愉快的收到响应的,☺

    相关文章

      网友评论

        本文标题:UmbrellaFramework(一)创建基础framewor

        本文链接:https://www.haomeiwen.com/subject/bpcaxxtx.html