正在加载今日诗词....
3 min read

组件化整体设计

iOS 应用组件化开发需要了解的知识... 目前正在补充中...
组件化整体设计

关键词

  • 依赖注入 DI
  • IoC 控制反转 Java
  • 通信: 中间者(target-action), url, 面向接口
  • pod 管理组件
  • 组件, 下沉基础组件, 公共组件, 业务组件, Foundation - UI
  • 模块, 业务模块
  • 其他

组件化通信方式

组件化的资源加载方式

静态库

问题

微信分享功能打包 pod 时,依赖 WechatOpenSDK , 主项目使用 use_frameworks, 导致, 无法编译通过

  1. 初步解决

Podspec 配置

  s.dependency 'WechatOpenSDK', '~> 1.8.2'

  s.pod_target_xcconfig = {
      'FRAMEWORK_SEARCH_PATHS' => '$(inherited) $(PODS_ROOT)/WechatOpenSDK',
      'OTHER_LDFLAGS'          => '$(inherited) -undefined dynamic_lookup'
  }

而主项目和各上层项目的 Podfile 中配置如下

 #静态库.a 在最后面添加如下内容:
    pre_install do |installer|
        # workaround for https://github.com/CocoaPods/CocoaPods/issues/3289
        Pod::Installer::Xcode::TargetValidator.send(:define_method, :verify_no_static_framework_transitive_dependencies) {}
    end
    
    post_install do |installer|
        installer.pods_project.targets.each do |target|
            target.build_configurations.each do |config|
                config.build_settings['ENABLE_BITCODE'] = 'NO'
            end
        end
    end

发现 Debug 环境下, 没有问题, 但是 Release 环境 启动就崩溃

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x000000012000d088
Triggered by Thread:  0

Filtered syslog:
None found

Dyld Error Message:
Dyld Message: Symbol not found: _OBJC_CLASS_$_SendAuthReq
  Referenced from: /private/var/mobile/Containers/Bundle/Application/A73C1507-D652-4A28-8658-AAB219F36379/iLawSchool.app/Frameworks/iLSWechatService.framework/iLSWechatService
  Expected in: flat namespace
 in /private/var/mobile/Containers/Bundle/Application/A73C1507-D652-4A28-8658-AAB219F36379/iLawSchool.app/Frameworks/iLSWechatService.framework/iLSWechatService
  Dyld Version: 370.1

Binary Images:

应该是动态库查微信SDK时,没有找到的原因.
目前的解决方案还是,全部把 pod 做成 s.static_framework = true

  1. Unable to run command 'StripNIB .nib' - this target might include its own product.

资源文件加载问题

方式1. 是将资源 打包到 bundle 中, 因此不需要担心重名的问题

podspec 配置如下

s.resource_bundles = {
    'ModuleA' => ['ModuleA/Resources/*.xcassets','ModuleA/Classes/**/**/*.xib']
  }

方式2. 是资源将打包到主bundle中, 所以不能有重名的出现

podspec 配置如下

  s.resources = ['ModuleA/Classes/**/**/*.xib']

如果修改后仍然报同样的错误, 可能是因为 Xcode 升级后的编译器问题, 可以将 Build System 切换为 New Build System . ps : 它会导致一些 pod 的头文件智能推荐失败的小 bug.

如果切换后发现报错,变为了

error: invalid task ('StripNIB.nib') with mutable output but no other virtual output node (in target '')

那么原因是因为在 podspec 文件中设置 source_filesresources 同时包含了 xib等资源文件

只需要修改如下

 s.source_files = 'ModuleA/Classes/**/*.{h,m}'

 s.resources = ['ModuleA/Classes/**/**/*.xib'] 或者 s.resource_bundles = {
    'ModuleA' => ['ModuleA/Resources/*.xcassets','ModuleA/Classes/**/**/*.xib']
  } 二选一 设置 xib 等资源

加载方式 见 custom pod loadBundleImage

操作时 注意清除缓存 以及 pod install

组件化整体构思

相关文章