Xcode14编译iOS11或iOS12报错dyld: Library not loaded: /usr/lib/swift/libswiftCoreGraphics.dylib 解决方案

在更新Xcode14之后发现编译项目在iOS12.5以上的系统都正常,但是在跑iOS12.5以下的系统,例如iOS11,和iOS12.1之类的系统会报错,报错如下:

dyld: Library not loaded: /usr/lib/swift/libswiftCoreGraphics.dylib
  Referenced from: /private/var/containers/Bundle/Application/xxx-xxx-xxx-xxx-xxxx/xxx.app/Frameworks/xxx.framework/xxx
  Reason: image not found

一看原因发现为image not found,但是项目中也没有用到这个库的image,而且在iOS12.5以上跑没有问题,所以显然不是这个问题,那么应该就是这个Library not loaded的问题。那么该问题的解决方案就是缺少那个library就在哪个库的 Targets-> Build Phase -> Link Binary with Libraries 中添加这个 libswiftCoreGraphics好了(报错是哪个库就加哪个)。例如我是缺少libswiftCoreGraphics,所以加一个libswiftCoreGraphics.tbd 就可以了;

注意还有一个前提:

那就是主工程中:Targets -> Build Settings ->Build Options -> Always Embed Swift Standard Libraries 需要设置为 Yes

方法二:(推荐方法) 在Podfile中统一修改

哪个库缺少就添加哪个库:

      if target.name.eql?('库名')

          libraries = config.build_settings['OTHER_LDFLAGS']

          config.build_settings['OTHER_LDFLAGS'] = "#{libraries} -lswiftCoreGraphics"

          libraryPath = config.build_settings['LIBRARY_SEARCH_PATHS']

          config.build_settings['LIBRARY_SEARCH_PATHS'] = "#{libraryPath} $(SDKROOT)/usr/lib/swift"

      end

与适配iOS16方法配合使用位置如下:(整体参考下面即可)

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
              config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '10.0'   

              config.build_settings['ENABLE_BITCODE'] = 'NO'

              config.build_settings['EXPANDED_CODE_SIGN_IDENTITY'] = ""

              config.build_settings['CODE_SIGNING_required'] = "NO"

              config.build_settings['CODE_SIGNING_ALLOWED'] = "NO"

              if target.name.eql?('库名') 

                libraries = config.build_settings['OTHER_LDFLAGS']

                config.build_settings['OTHER_LDFLAGS'] = "#{libraries} -lswiftCoreGraphics"

                libraryPath = config.build_settings['LIBRARY_SEARCH_PATHS']

                config.build_settings['LIBRARY_SEARCH_PATHS'] = "#{libraryPath} $(SDKROOT)/usr/lib/swift"

              end

        end
    end
end

方法三:对方法二的补充(如上述方法二还是会报错,可以使用以下方法

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
              config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '10.0'   

              config.build_settings['ENABLE_BITCODE'] = 'NO'

              config.build_settings['EXPANDED_CODE_SIGN_IDENTITY'] = ""

              config.build_settings['CODE_SIGNING_required'] = "NO"

              config.build_settings['CODE_SIGNING_ALLOWED'] = "NO"

              if target.name.eql?('库名')

                   # Compose .xcconfig file path

                   configuration_file = "#{Dir.pwd}/Pods/Target Support Files/#{target.name}/#{target.name}.#{config}.xcconfig"

                   # Read values from the .xcconfig file

                   puts "#{target.name}.#{config}.xcconfig PATH #{configuration_file}"

                   libraries = read_xxconfig_Param(configuration_file,'OTHER_LDFLAGS')

                   libraryPath = read_xxconfig_Param(configuration_file,'LIBRARY_SEARCH_PATHS')

                   puts "#{target.name}.#{config}.xcconfig libraries #{libraries}"

                   config.build_settings['OTHER_LDFLAGS'] = "#{libraries} -lswiftCoreGraphics"

                   config.build_settings['LIBRARY_SEARCH_PATHS'] = "#{libraryPath} $(SDKROOT)/usr/lib/swift"

              end

        end
    end
end

def read_xxconfig_Param(filePath,paramName)

       paramValue = ""

       if File::exist?(filePath)

            IO.readlines(filePath).each do |block|

                str = String.new(block)

                if str.include? paramName

                     paramValue = str.gsub("#{paramName} = ","")

                     return paramValue

                 end

            end

        end

        return ""

end

最后记得改完后要重新 pod install

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...