来到了新公司,要开发的第一个项目据说可能要封成framework,可是我从来没自己做过framework呀!顿时开始发愤图强,赶紧恶补了起来。但是还是遇到了一些乱七八糟的情况,所以写个随笔记下来。
有哪里不对的,大家可以温柔的指出哈~
零、准备工作
1.新建工程,选择iOS->Framework&Library->Cocoa Touch Framework,命名为FramewrokTest.
2.新建FrameworkManager和TestViewController(Also create XIB file),并添加进来一张图片,我的是yumy.JPG.
3.开始写代码
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h>@interface FrameworkManager : NSObject+(NSString*)presentTestVCFromCurrentVC:(UIViewController *)VC;@end
#import "FrameworkManager.h" #import "TestViewController.h"@implementation FrameworkManager+(NSString *)presentTestVCFromCurrentVC:(UIViewController *)VC{TestViewController *testVC = [[TestViewController alloc] initWithNibName:@"FrameworkBundle.bundle/TestViewController" bundle:nil];[VC presentViewController:testVC animated:YES completion:nil];return @"调用成功!"; }@end
#import "TestViewController.h"@interface TestViewController ()@property (weak, nonatomic) IBOutlet UIImageView *imgView;@end@implementation TestViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view from its nib.self.imgView.image = [UIImage imageNamed:@"FrameworkBundle.bundle/yumy.JPG" inBundle:nil compatibleWithTraitCollection:nil];self.imgView.highlightedImage = [UIImage imageNamed:@"FrameworkBundle.bundle/haha.JPG" inBundle:nil compatibleWithTraitCollection:nil]; } - (IBAction)changeImg:(id)sender { self.imgView.highlighted = !self.imgView.highlighted; } - (IBAction)back:(id)sender { [self dismissViewControllerAnimated:YES completion:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
一、创建bundle
1.创建bundle,选中PROJECT之后,点击加号选择macOS->Framework&Library->Bundle,命名为FrameworkBundle
之后将FrameworkBundle->Build Settings->Build Options->Enable Bitcode设置为NO.
因为bundle默认是macOS的所以要改成iOS的,需要将FrameworkBundle->Build Settings->Architectures->Base SDK改为Latest iOS
分别将FrameworkBundle->Build Settings->Deployment下的iOS Deployment Target和macOS Deployment Target,选择为你最低支持的版本
将FrameworkBundle->Build Settings->User-Defined->COMBINE_HIDPI_IMAGES设置为NO(此属性在Base SDK 选择了iOS SDK之后才会出现)
2.选中刚刚创建的FrameworkBundle->Build Phases->Copy,将所有的图片和xib添加到copy bundle resources中,如下图。
3.选择FrameworkBundle和Generic iOS Device,之后Command+B编译Bundle。(注意:伺候只要是xib有改变或是新加了图片等等都需要重新编译 Bundle)
编译之前,可以看到Products下的FrameworkBundle.bundle还是红色的,编译后就变成了黑色,变成黑色后选中FrameworkBundle.bundle选择show in finder,之后就可以把bundle加到主工程中啦!(具体的下面会说)
二、使用脚本自动合并framework
1.事前准备都做完了,现在开始配置了!
1)Targets->FrameworkTest->General->Deployment Info->Deployment Target设置成你支持的最低版本
2)给FrameworkTest->Build Settings->Architectures->Architectures添加上armv7s,下图是添加完的样子。
3)按下图设置红框中的字段
4)在FrameworkTest->Build Phases->Headers中,将想要让静态库使用者看到的头文件放到public中。(FrameworkTest.h默认必须在Public中)
2.选中FrameworkTest点击加号选择Cross-platform->Other->Aggregate,命名为FrameworkAggregate。
3.选中FrameworkAggregate->Build Phases,点击加号,添加New Run Script Phases。
之后将下面的脚本添加到Run Script中
#指定 FrameWork 与 bundle的target名称FrameWork_NAME="FrameworkTest" Bundle_NAME="FrameworkBundle"#打包结果存储地址 Products_DIR=${SRCROOT}/Products/#定义framework与bundle存储路径 FRAMEWORK_INSTALL_DIR=${SRCROOT}/Products/${FrameWork_NAME}.framework Bundle_INSTALL_DIR=${SRCROOT}/Products/${Bundle_NAME}.bundle#将要生成包的路径 WRK_DIR=buildFRAMEWORK_DEVICE_DIR=${WRK_DIR}/Release-iphoneos/${FrameWork_NAME}.framework FRAMEWORK_SIMULATOR_DIR=${WRK_DIR}/Release-iphonesimulator/${FrameWork_NAME}.frameworkBundle_SIMULATOR_DIR=${WRK_DIR}/Release-iphonesimulator/${Bundle_NAME}.bundle#清理下工程 # -configuration ${CONFIGURATION} # Clean and Building both architectures. xcodebuild -configuration "Release" -target "${FrameWork_NAME}" -sdk iphoneos clean build xcodebuild -configuration "Release" -target "${FrameWork_NAME}" -sdk iphonesimulator clean buildxcodebuild -configuration "Release" -target "${Bundle_NAME}" -sdk iphoneos clean build xcodebuild -configuration "Release" -target "${Bundle_NAME}" -sdk iphonesimulator clean build#清除下存储路径下的旧包 if [ -d "${FRAMEWORK_INSTALL_DIR}" ] then rm -rf "${FRAMEWORK_INSTALL_DIR}" fi mkdir -p "${FRAMEWORK_INSTALL_DIR}"if [ -d "${Bundle_INSTALL_DIR}" ] then rm -rf "${Bundle_INSTALL_DIR}" fi mkdir -p "${Bundle_INSTALL_DIR}"#随便拷贝一个.framework cp -R "${FRAMEWORK_DEVICE_DIR}/" "${FRAMEWORK_INSTALL_DIR}/" # Uses the Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product. #合并.framework中的二进制文件,讲通用二进制文件替换 lipo -create "${FRAMEWORK_DEVICE_DIR}/${FrameWork_NAME}" "${FRAMEWORK_SIMULATOR_DIR}/${FrameWork_NAME}" -output "${FRAMEWORK_INSTALL_DIR}/${FrameWork_NAME}"#拷贝bundle cp -R "${Bundle_SIMULATOR_DIR}" "${Products_DIR}/"rm -r "${WRK_DIR}" open "${Products_DIR}"
4.最后选择FrameworkAggregate和Generic iOS Device,之后Command+B编译,编译成功会弹出窗口
三、framework(带有bundle)的使用
1.新建工程MainProject,选择Single View App就行之后将之前编译成功的FrameworkBundle.bundle和FrameworkTest.framework加到MainProject中。
2.配置工程
1)将Bitcode设置为NO
2)在MainProject->Build Phases->Copy Bundle Resources中点击加号添加FrameworkTest.framework。
其他的配置该怎么配怎么配
3.现在来调用一下framework中的方法,实现在Main.storyboard中放一个按钮,点击这个按钮调用FrameworkManager中提供的方法跳转到TestViewController。
ViewController中的调用如下
#import "ViewController.h" #import <FrameworkTest/FrameworkTest.h>@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. }- (IBAction)presentVC:(id)sender { // [SDK2Manager presentCurrentVC:self]; [FrameworkManager presentTestVCFromCurrentVC:self]; }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }@end
好啦~完事啦~运行一下看看结果吧
四、在已有项目中创建framework
1.创建一个新项目FrameworkInMainProject,选Single View App就行,之后设置最低支持版本
2.选中project之后点击下方加号,添加framework,bundle,aggregate,依次命名为ProjectFramework,ProjectBundle,ProjectAggregate
下图为添加完的样子
3.之后在FrameworkInMainProject->Build Phases->Target Dependencies中添加ProjectBundle,ProjectFramework
4.以上三小点都做完了,就可以参照文章前面讲的:
零、准备工作
一、创建bundle
二、使用脚本自动合并framework
三、framework(带有bundle)的使用
这四部分配置framework,bundle和aggregate了。想要看到效果,就正常运行项目即可(之前把framework和bundle先build一下)。aggregate是当你需要导出framework,bundle时才需要build,平时不需要。
ProjectFramework文件夹如下图
转载于:https://www.cnblogs.com/justWWJBlog/p/9040565.html