前语

iOS开发的过程中,经常会使用Framework静态库。今日主要讲解一下如何在开发Framework的时分,用pod来依靠。当然,能够直接将pod库再用内部依靠的方法引进,但是这种依靠风险很大。比方Framework内部引证了YYModel,那么在不修正前缀或者替换函数名称。在其他人引证Framework时,会带来极大的兼容性问题。所以,仍是主张外部依靠最为稳妥。

创立Framework

1.示例起名:TestKit,能够根据本身需求来命名。

iOS 创建framework依赖cocoapods

2.设置最低版别

iOS 创建framework依赖cocoapods

3.TARGETS->Build Settings->Mach-O Type,设为 Static Library。

iOS 创建framework依赖cocoapods

4.Build Libraries for Distribution 设置为Yes (如果是swift语言的时分需求)

iOS 创建framework依赖cocoapods

5.增加pod文件

platform :ios, '11.0'
post_install do |installer|
  installer.pods_project.build_configurations.each do |config|
    config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64"
  end
end
target 'TestKit' do
  pod 'SVProgressHUD'
end

6.创立调用代码 .h + .m

#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface TestShowTool : NSObject
+ (void)test_showInfo:(NSString *)info;
@end
NS_ASSUME_NONNULL_END
#import "TestShowTool.h"
#import <SVProgressHUD.h>
@implementation TestShowTool
+ (void)test_showInfo:(NSString *)info{
  [SVProgressHUD showInfoWithStatus:info];
}
@end

7.将.h文件露出为public

iOS 创建framework依赖cocoapods

8.TestKit.h声明想要露出的Header类

iOS 创建framework依赖cocoapods

生成构建结构

在模拟器和真机状态下编辑一遍,保证成功

iOS 创建framework依赖cocoapods

iOS 创建framework依赖cocoapods

生成通用结构-> 主动生成Framework

1.TARGETS 左下角加号,增加一个 Aggregate,命名为 TestMark

iOS 创建framework依赖cocoapods

2.Build Phases->左上角加号->New Run Script Phase

iOS 创建framework依赖cocoapods

3.增加如下代码,将 /Users/xxx/Desktop 中的xxx 替换为你自己的Mac名称输出到桌面,也能够输入 pwd查看。

if [ "true" == ${ALREADYINVOKED:-false} ]
then
echo "RECURSION: Detected, stopping"
else
export ALREADYINVOKED="true"
UNIVERSAL_OUTPUTFOLDER=/Users/xxx/Desktop
# 输出文件夹
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"
xcodebuild -target "${TARGET_NAME}" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos  BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" build
xcodebuild -target "${TARGET_NAME}" -configuration ${CONFIGURATION} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" build
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework" "${UNIVERSAL_OUTPUTFOLDER}/"
SIMULATOR_SWIFT_MODULES_DIR="${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule/."
if [ -d "${SIMULATOR_SWIFT_MODULES_DIR}" ]; then
cp -R "${SIMULATOR_SWIFT_MODULES_DIR}" "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule"
fi
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework/${PROJECT_NAME}"
fi
  1. 确定生成构建结构后,选择Target为TestMark。command + B 即可在桌面看到生成Framework了。

iOS 创建framework依赖cocoapods

iOS 创建framework依赖cocoapods