美文网首页
iOS架构之路07 -- .framework静态库依赖Coco

iOS架构之路07 -- .framework静态库依赖Coco

作者: YanZi_33 | 来源:发表于2021-09-15 18:03 被阅读0次
    • .framework工程需依赖Masonry库进行UI布局,cd 到工程路径下 执行pod init 生成Podfile文件,然后编辑Podfile文件,如下所示:
    # Uncomment the next line to define a global platform for your project
    # platform :ios, '9.0'
    
    target 'FMyCategory' do
      # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
      use_frameworks!
      # Pods for FMyCategory
      pod 'Masonry'
    
    end
    
    target 'FMyCategoryBundle' do
      # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
      # use_frameworks!
    
      # Pods for FMyCategoryBundle
    end
    
    • target 'FMyCategory' do 中加入 pod 'Masonry'
    • 必须加上use_frameworks!,表明pod进来的第三方库以.framework的形式集成,否则会以.a的形式集成;
    • 然后执行pod install
    • 再创建一个脚本文件ScriptPod.sh,脚本内容如下:
    #!/bin/sh
    
    #  ScriptPod.sh
    #  FMyCategory
    #
    #  Created by liyanyan33 on 2021/9/10.
    #  
    #!/bin/sh
    
    #工程名
    PROJECT_NAME=FMyCategory
    
    #工作空间
    WORKSPACE_NAME=${PROJECT_NAME}.xcworkspace
    
    #工程路径
    PROJECT_DIR=$(pwd)
    
    #build之后的文件夹路径
    BUILD_DIR=$PROJECT_DIR/Build/Products
    BUILD_ROOT=$PROJECT_DIR/Build/Products
    
    #打包模式 Debug/Release 默认是Release
    development_mode=Release
    
    #
    UNIVERSAL_OUTPUTFOLDER=${PROJECT_DIR}/${development_mode}-universal
    INSTALL_DIR_A=${PROJECT_DIR}/${PROJECT_NAME}.framework/${PROJECT_NAME}
    
    
    # make sure the output directory exists
    mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"
    
    
    # Step 1. Build Device and Simulator versions
    xcodebuild -workspace "${WORKSPACE_NAME}" -scheme "${PROJECT_NAME}" -configuration ${development_mode} -sdk iphoneos ONLY_ACTIVE_ARCH=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean
    
    xcodebuild -workspace "${WORKSPACE_NAME}" -scheme "${PROJECT_NAME}" -configuration ${development_mode} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean
    
    
    xcodebuild -workspace "${WORKSPACE_NAME}" -scheme "${PROJECT_NAME}" -configuration ${development_mode} -sdk iphoneos ONLY_ACTIVE_ARCH=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" build
    
    xcodebuild -workspace "${WORKSPACE_NAME}" -scheme "${PROJECT_NAME}" -configuration ${development_mode} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" build
    
    
    # Step 2. Copy the framework structure (from iphoneos build) to the universal folder
    cp -R "${BUILD_DIR}/${development_mode}-iphoneos/${PROJECT_NAME}.framework" "${UNIVERSAL_OUTPUTFOLDER}/"
    
    
    # Step 4. Create universal binary file using lipo and place the combined executable in the copied framework directory
    lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${development_mode}-iphonesimulator/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}"
    
    echo "======合成结束======"
    
    # Step 5. Convenience step to copy the framework to the project's directory
    cp -R "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework" "${PROJECT_DIR}"
    
    # -f 判断文件是否存在
    if [ -f "${INSTALL_DIR_A}" ]
    then
    echo "======验证合成包是否成功======"
    lipo -info "${INSTALL_DIR_A}"
    fi
    
    #打开目标文件夹
    open "${PROJECT_DIR}"
    
    
    • 此时我们的工程结构与本地文件目录如下所示:
    image.png image.png
    • 其中SFViewController.m文件中,引用了Masonrypod库进行UI布局,代码如下所示:
    #import "SFViewController.h"
    #import "Masonry.h"
    
    @interface SFViewController ()
    
    @property(nonatomic,strong)UIImageView *im;
    
    @property(nonatomic,strong)UILabel *label;
    
    @end
    
    @implementation SFViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor = [UIColor whiteColor];
        
        NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"FMyCategoryBundle" ofType:@"bundle"]];
        NSString *path = [[bundle resourcePath] stringByAppendingPathComponent:@"sf_navBack_hl"];
        
        self.im = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
        self.im.image = [UIImage imageWithContentsOfFile:path];
        [self.view addSubview:self.im];
        
        self.label = [[UILabel alloc]init];
        self.label.text = @"SFViewController vs MASConstraintMaker";
        self.label.textColor = [UIColor redColor];
        self.label.font = [UIFont systemFontOfSize:18];
        [self.view addSubview:self.label];
        //使用Masonry布局
        [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.view);
        }];
    }
    @end
    
    • 最后在工程路径下,执行ScriptPod.sh脚本 终端执行./ScriptPod.sh,执行结果如下所示:
    image.png image.png
    • 创建建一个新的工程Test_FrameworkPodsProject,然后cd 到工程路径下执行pod init,生成Podfile文件,然后编辑Podfile文件,如下所示:
    # Uncomment the next line to define a global platform for your project
    # platform :ios, '9.0'
    
    use_frameworks!
    
    target 'Test_FrameworkPodsProject' do
      # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
      
      # Pods for Test_FrameworkPodsProject
      pod 'Masonry'
    
    end
    
    • 然后执行pod install
    • 再将上面的两个文件FMyCategory.frameworkFMyCategoryBundle.bundle直接拖到Test_FrameworkPodsProject工程中,如下所示:Build Phase中都是默认配置
    image.png
    • 然后编写代码,调用函数,如下:
    image.png
    • 注意⚠️:FMyCategory.framework静态库并没有将Masonry库的代码文件打包进来,只是对Masonry有引用而已,Masonry库的代码最终被打包进入的是主工程Test_FrameworkPodsProject,FMyCategory.framework静态库被主工程Test_FrameworkPodsProject使用,就可以依赖Masonry库了。

    相关文章

      网友评论

          本文标题:iOS架构之路07 -- .framework静态库依赖Coco

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