第一步:创建framworking工程
第二步
在工程里封装一个自定义类;
#import <UIKit/UIKit.h>
@interface imageview : UIImageView
-(instancetype)initWithFrame:(CGRect)frame urlString:(NSString *)_urlString;
@end
#import "imageview.h"
@implementation imageview
-(instancetype)initWithFrame:(CGRect)frame urlString:(NSString *)_urlString{
self = [super init];
if (self) {
self.layer.cornerRadius = 10;
self.layer.masksToBounds = YES;
self.frame = frame;
NSURL *url = [NSURL URLWithString:_urlString];
dispatch_queue_t queuewww=dispatch_queue_create("test", NULL);
dispatch_async(queuewww, ^{
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *imga = [UIImage imageWithData:data];
dispatch_sync(dispatch_get_main_queue(), ^{
self.image = imga;
});
});
}
return self;
}
将 Header / Project中的.h文件拖入Public中
如下图
设置Build Settings
a.将Linking / Mach Type 设置为 Static Library
b.将Packaging / Defines Module 设置为NO;
如下图
第三步,添加Aggregate
在Aggregate 的Build Phase添加Run Script并修改其中的shell 对话框内容
如图
内容如下:修改FMK_NAME值为自己自定义类名;如FMK_NAME = ...
# Sets the target folders and the final framework product.
# 如果工程名称和Framework的Target名称不一样的话,要自定义FMKNAME
# 例如: FMK_NAME = "MyFramework"
FMK_NAME=Myframworking
# 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}"
第四步:
(将模拟器调为Generic IOS Device模式)
1.先运行framworking
2.在运行Aggregate
之后会弹出新创建的自定义库文件夹。
恭喜你已经成功创建属于自己的框架。
若需调用框架,只需要将自定义库文件夹中product中的程序拖入你的X-code工程就可以使用了;