静态sdk引入外部framework

前言,这几天突然遇到一个问题,那就是维护的sdk单个framework已经超过了40M,这个已经打过了可以推送到git服务器的限制,

error: RPC failed; HTTP 403 curl 22 The requested URL returned error: 403
send-pack: unexpected disconnect while reading sideband packet
fatal: the remote end hung up unexpectedly

方案一:

一个解决方案是找运维将限制放开,这个虽然没有技术含量,但是由于各种跨部门协助审批很麻烦,而且如果维护的sdk不断集成了越来越多的framework,还会不断增大,所以这个方法不是一个很好

方案二:

sdk的podfile依赖文件使用!useframeworks,这种方式可以动态添加依赖库,但是由于项目历史原因,以及一些其他适配原因,没有使用这个,导致所有依赖的库都全包打包进了维护的sdk,这个方案对于当下项目不可选

方案三:借用网上的方案

iOS SDK 动态引入第三方SDK,这个方案虽然可行,但是太繁琐,适配起来太麻烦

方案四:使用pod依赖sdk+依赖service+部分源文件解决

思路:将核心代码打成sdk,不想打包进sdk的三方库使用podspec的库依赖,依赖三方库的代码抽取成一个单独的文件,然后podspec单独引入这个抽取的文件,sdk里通过动态库的方式引入这个单独的源文件实现瘦身的效果


podspec文件核心内容

Pod::Spec.new do |s|
  s.name             = 'xxxx'
  s.version          = '1.0.0.1'
  s.summary          = 'xxx framework.'

  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'http://xxxx/xxx/xxxx'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'xxx' => '[email protected]' }
  s.source           = { :git => 'http://xxx/xxx/xxx.git', :tag => s.version.to_s }

  s.ios.deployment_target = '12.0'


# 依赖的打好包的framework
  s.vendored_frameworks = [
  'Resource/xxx/xxx.xcframework'
  ]
  # 动态依赖三方库的格外的源码文件
  s.source_files = "Resource/xxx/**/*.{h,m}"
  # 依赖的字体、图片、音乐等资源
  # s.resources = ['Resource/xxx/*']

  s.requires_arc = true
  s.static_framework = true

  # 需要依赖的三方库
  # s.dependency 'AFNetworking', '~>4.0.1'
  s.dependency 'TWTRKit'

end

格外源文件的代码:

#import "TestTwitter.h"

@import TwitterKit;
// 遵循Twitter的某个协议,以及使用Twitter的某个类的对象最为属性
@interface Test()<这儿是Twitter的某个协议>
@property (nonatomic, strong) TWTRTwitter *twitter;
// 跟Twitter没关系的属性
@property (nonatomic, copy) NSString *message;
@end


@implementation TestTwitter

/// 调用Twitter的方法
- (void)loginByTwitter {
    [[Twitter sharedInstance] logInWithCompletion:^(TWTRSession * _Nullable session, NSError * _Nullable error) {
        
    }];
}

@end

sdk里通过协议或则NSClassFromString或者使用参数传入这个类都可以


核心的点在于:vendored_frameworkssource_filesdependency一起使用,使用源码跳过编译阶段的检查,动态引入

你可能感兴趣的:(静态sdk引入外部framework)