本文内容主要是:打包Framework、打包后的测试。
打包Framework
1.新建静态库项目iOS--Framework&Library--Cocoa Touch Framework
设置项目名为MyFramework,项目创建完成后在Xcode中的目录结构如下图
2.向MyFramework静态库项目中添加需要打包的所有文件
本文中向其中添加MyLog.h和MyLog.m两个文件
MyLog.h
//
// MyLog.h
// MyFramework
//
// Created by zym on 2019/3/12.
// Copyright © 2019 zym. All rights reserved.
//
#ifndef MyLog_h
#define MyLog_h
#endif /* MyLog_h */
#import <Foundation/Foundation.h>
@interface MyLog : NSObject
-(void) print;
@end
MyLog.m
//
// MyLog.m
// MyFramework
//
// Created by zym on 2019/3/12.
// Copyright © 2019 zym. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MyLog.h"
@implementation MyLog
-(void) print
{
NSLog(@"Hello World!");
}
@end
3.选中Xcode中MyFramework项目名,在右侧TARGETS中选择MyFramework--Build Phases--Headers
将需要公开的.h头文件通过"+"添加到public中,将其它的.h头文件通过"+"添加到project中。本文中将MyLog.h头文件添加到public中,如下图所示
4.在创建MyFramework项目时生成的MyFramework.h文件中添加公开的.h头文件import
5.更改相关参数
选择TARGETS中的MyFramework--Build Settings--Architectures,支持的处理器架构中添加armv7s(即支持iphone5、iphone5s及以上)
选择TARGETS中的MyFramework--Build Settings--搜索Mach-O Type--设置为Static Library
选择TARGETS中的MyFramework--Build Settings--搜索Linking--设置为Dead Code Stripping 为NO--设置Link With Standard Libraries为NO
选择TARGETS中的MyFramework--Build Settings--Deployment--iOS Deployment Target 设置项目对IOS最低版本的支持为8.0
6.创建Aggregate
选中项目MyFramework,执行File--new--Target
选择Cross-platform---Aggregate
7.在新建的MyAggregate中添加New Run Script Phase
选择TARGETS--Build Phases--"+"--New Run Script Phase
新建的Run Script如下图
在脚本框中添加如下脚本
# 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}"
8.将当前编译的Target选择为MyFramework,点击MyFramework--Edit Scheme
将Debug修改为Release
9.将编译的Target选择为MyAggregate,设备Devices选择Generic iOS Device
同理,点击MyAggregate--Edit Scheme,将Debug修改为Release
10.在编译之前将编译的Target选择为MyAggregate,执行Command+B开始编译。显示build success,并自动打开创建的framework包,显示包中的内容。
打开上图中的Headers文件夹,发现2项内容,其中就包括MyLog.h
打包后的测试
1.新建一个工程iOS--Single View App
创建完成后,在Xcode中的目录结构如下图
2.将刚才创建的MyFramework.framework包拖拽到MyDemo项目中
展开MyFramework.framework,发现Headers中存在2个头文件
3.修改ViewController.m,引入MyFramework.framework包中的MyLog.h,创建MyLog对象并调用print方法
//
// ViewController.m
// MyDemo
//
// Created by zym on 2019/3/12.
// Copyright © 2019 zym. All rights reserved.
//
#import "ViewController.h"
#import <MyFramework/MyLog.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MyLog *myLog=[[MyLog alloc] init];
[myLog print];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
4.运行MyDemo
build success并在模拟器中显示空白页面,如下图
控制台输出Hello World,说明MyDemo项目调用我们打成的MyFramework.framework包成功