iOS cocoapods 配置MRC (-fno-objc-arc)

148 阅读1分钟

一、在 Podfile 中指定

在 Podfile 中,你可以为特定的库指定编译器标志。这可以通过 pod 命令的 :configuration 选项来实现。例如,如果你想为名为 SomeLibrary 的库禁用 ARC,可以这样做:

pod 'SomeLibrary', :configuration => ['Debug', 'Release'], :inhibit_warnings => true do |config|
  config.build_settings['CLANG_ENABLE_OBJC_ARC'] = 'NO'
end

二、Pod 私有库中配置MRC -- 配置Podspec文件

1、配置全部文件为MRC

在你的Podspec文件中,你可以通过设置xcconfig来指定编译选项。对于MRC,你需要确保编译器知道如何处理MRC代码。

例如,如果你的私有库中的某个类使用了MRC,你可以在Podspec文件中添加如下配置:

Pod::Spec.new do |s|
  s.name             = 'YourPodName'
  s.version          = '1.0.0'
  s.summary          = 'A short description of YourPodName.'
  s.homepage         = 'https://github.com/your/repo'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'Author Name' => 'author@example.com' }
  s.source           = { :git => 'https://github.com/your/repo.git', :tag => s.version.to_s }
  s.ios.deployment_target = '9.0'
  s.source_files = 'YourPodName/Classes/**/*.{h,m}'
 
  s.pod_target_xcconfig = { 
    'CLANG_ENABLE_OBJC_ARC' => 'NO', # 如果你有ARC代码也需要编译,可以单独设置文件
    'OTHER_CFLAGS' => '-fno-objc-arc' # 针对所有Objective-C文件禁用ARC
  }
end
2、在podspec中指定requires_arc:false,同时指定requires_arc的文件,eg:

这样写的好处是别的项目拉取到你pod源码的时候,不会多出来一个包含MRC文件的文件夹,看起来代码比较完整;

s.requires_arc = false
s.requires_arc = ['Classes/ARC/**/*.m']

如果不指定requires_arc文件就会全部设置为-fno-objc-arc。

3、在subsepc中指定非ARC
non_arc_files = 'VPhoneConnect/Classes/VPNotification/Socket/Message.pbobjc.m'
  
# 子模块
s.subspec 'VPNotification' do |sd|
 sd.source_files = non_arc_files
    #此库为MRC,非ARC
 sd.requires_arc = false
end

s.subspec 'VPNotification' do |sd| 中的 VPNotification 为MRC文件所在文件夹。

完成后pod install