此处说的可拆卸,意思就是业务App的工程是否导入这个framework都不影响编译。若是业务导入了这个framework,就可使用其中功能,若是没导入,也能编译经过。git
在咱们开发项目的过程当中,会导入不少的三方库,好比:会导入公司内部业务封装的、微信、微博和支付宝等相关的framework等。在稍微复杂一点的业务中,以下图:github
其中,A.framework和B.framework都是静态库,A.framework使用了B.framework中的方法,那么,通常状况下在APP中想要使用A.framework中的方法,必需要同时将A.framework和B.framework导入到APP工程中,不然编译时会报错。可是在现实状况中,可能业务不须要A包中涉及到B包的功能,所以只想导入A.framework且不想导入B.framework。objective-c
咱们新建了一个下面的工程,工程中有两个framework,示例中APP直接使用TestDynamicSdk的方法,TestDynamicSdk使用TestStaticSdk的方法。微信
如下代码工做在TestDynamicSdk中,咱们以TestDynamicSdk做为上述A.framework的角色作说明:函数
#import "TestDynamicSdk.h"
//// __has_include() 宏在导入三方库 .h 过程当中的使用
#if __has_include(<TestStaticSdk/TestStaticSdk.h>)
#import <TestStaticSdk/TestStaticSdk.h>
#ifndef HAS_IMPORT_DY
#define HAS_IMPORT_DY 1
#endif
#else
#ifndef HAS_IMPORT_DY
#define HAS_IMPORT_DY 0
#endif
#endif
@implementation TestDynamicSdk
//// 👆上面定义的宏HAS_IMPORT_DY的使用
- (NSString *)getCombineStrWithA:(NSString *)aStr B:(NSString *)bStr {
#if HAS_IMPORT_DY == 1
TestStaticSdk *staticSdk = [[TestStaticSdk alloc] init];
NSString *combinedStr = [staticSdk getCombineStrWithA:@"common_A_String" B:@"common_B_String"];
return combinedStr;
#else
return nil;
#endif
}
@end
复制代码
这样APP便可在使用TestDynamicSdk时自由拆卸TestStaticSdk.framework。工具
#import "TestDynamicSdk.h"
@interface TestDynamicSdk ()
@property (nonatomic, strong) id staticSdkObject;
@end
@implementation TestDynamicSdk
- (id)init {
self = [super init];
if (self) {
Class class = NSClassFromString(@"TestStaticSdk");
if (!class) {
NSLog(@"TestStaticSdk没有编译");
return self;
}
SEL sel = NSSelectorFromString(@"new");
id (*imp)(id, SEL) = (id (*)(id, SEL))[class methodForSelector:sel];
self.staticSdkObject = imp(class, sel);
}
return self;
}
- (NSString *)getCombineStrWithA:(NSString *)aStr B:(NSString *)bStr {
if (!_staticSdkObject) {
NSLog(@"staticSdkObject为空");
return nil;
}
SEL sel = NSSelectorFromString(@"getCombineStrWithA:B:");
if (![_staticSdkObject respondsToSelector:sel]) {
NSLog(@"getCombineStrWithA:B:方法没有实现");
return nil;
}
NSString * (*imp)(id, SEL, NSString *, NSString *) = (NSString * (*)(id, SEL, NSString *, NSString *))[_staticSdkObject methodForSelector:sel];
NSString *combinedStr = imp(_staticSdkObject, sel, aStr, bStr);
return combinedStr;
}
@end
复制代码
这样也能实现APP在使用TestDynamicSdk时自由拆卸TestStaticSdk.framework。优化
注意:atom
GitHub源码spa
小编微信:可加并拉入《QiShare技术交流群》。
关注咱们的途径有:
QiShare(简书)
QiShare(掘金)
QiShare(知乎)
QiShare(GitHub)
QiShare(CocoaChina)
QiShare(StackOverflow)
QiShare(微信公众号)
推荐文章:
自定义WKWebView显示内容(一)
Swift 5.1 (6) - 函数
Swift 5.1 (5) - 控制流
Xcode11 新建工程中的SceneDelegate
iOS App启动优化(二)—— 使用“Time Profiler”工具监控App的启动耗时
iOS App启动优化(一)—— 了解App的启动流程
iOS WKWebView的基本使用
Swift 5.1 (4) - 集合类型
奇舞周刊