iOS架构之路07 -- .framework静态库依赖Coco
2021-09-15 本文已影响0人
YanZi_33
- 在自己创建的.framework工程中,会依赖其他第三方库pod库,本文是在 iOS架构之路04 -- 静态库.framwork的手动,脚本,资源文件打包 这篇文章所建工程的基础上展开的,原来的工程结构如下:
25440976-e44649c4aa75a2a1.png
- .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}"
- 此时我们的工程结构与本地文件目录如下所示:
- 其中
SFViewController.m
文件中,引用了Masonry
pod库进行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
,执行结果如下所示:
- 创建建一个新的工程
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.framework
与FMyCategoryBundle.bundle
直接拖到Test_FrameworkPodsProject
工程中,如下所示:Build Phase
中都是默认配置
- 然后编写代码,调用函数,如下:
- 注意⚠️:FMyCategory.framework静态库并没有将
Masonry
库的代码文件打包进来,只是对Masonry
有引用而已,Masonry
库的代码最终被打包进入的是主工程Test_FrameworkPodsProject
,FMyCategory.framework静态库被主工程Test_FrameworkPodsProject
使用,就可以依赖Masonry
库了。