26

多亏了Catalyst,终于可以将应用程序移植到 mac,但问题是,许多 pod 不支持 AppKit。最常见的是 Crashlytics / Firebase。

In [...]/Pods/Crashlytics/iOS/Crashlytics.framework/Crashlytics(CLSInternalReport.o), building for Mac Catalyst, but linking in object file built for iOS Simulator, file '[...]/Pods/Crashlytics/iOS/Crashlytics.framework/Crashlytics' for architecture x86_64

由于这是一个最近的话题,我找不到关于如何从我的 macOS 构建中删除 pod 但保留它用于 iOS 和 iPadO S的文档。

可以在代码中使用:

#if !targetEnvironment(macCatalyst) 
// Code to exclude for your macOS app
#endif

但是问题的一部分,另一部分是仅针对 iOS 链接 pod ......

当库对 macOS 并不重要但在 iOS 上仍然需要时,最简单/最好的做法是什么?

4

6 回答 6

24

对于为 Catalyst 处理不受支持的框架的最佳方法,你们应该阅读Fernando Moya de Rivas的解决方案,他有一个 github,这里有一个包含更多最新信息的解决方案。

他基本上说你只需要定义一个你不想在mac osx上安装的所有库的数组,就像这样:['Fabric', 'Crashlytics', 'Firebase/Core', ...].

然后,您的 pod 文件看起来很简单:

# Podfile
load 'remove_unsupported_libraries.rb'

target 'My target' do
   use_frameworks!
   # Install your pods
   pod ...
end

# define unsupported pods
def catalyst_unsupported_pods
    ['Fabric', 'Crashlytics', 'Firebase/Core', ...]
end

# Remove unsupported pods from your project
post_install do |installer|   
    installer.configure_support_catalyst
end
于 2020-03-10T07:16:03.107 回答
19

在@ajgryc 回答之后,我能够做出一个时尚的解决方案:

在你的 podfile 添加

post_install do |installer|
    installer.pods_project.targets.each do |target|
        if target.name == "Pods-[Name of Project]"
            puts "Updating #{target.name} OTHER_LDFLAGS to OTHER_LDFLAGS[sdk=iphone*]"
            target.build_configurations.each do |config|
                xcconfig_path = config.base_configuration_reference.real_path
                xcconfig = File.read(xcconfig_path)
                new_xcconfig = xcconfig.sub('OTHER_LDFLAGS =', 'OTHER_LDFLAGS[sdk=iphone*] =')
                File.open(xcconfig_path, "w") { |file| file << new_xcconfig }
            end
        end
    end
end

从 Cocoapods 1.8.4 开始

post_install do |installer|
  installer.pods_project.targets.each do |target|
    if target.name == "Pods-[Name of Project]"
      puts "Updating #{target.name} to exclude Crashlytics/Fabric"
      target.build_configurations.each do |config|
        xcconfig_path = config.base_configuration_reference.real_path
        xcconfig = File.read(xcconfig_path)
        xcconfig.sub!('-framework "Crashlytics"', '')
        xcconfig.sub!('-framework "Fabric"', '')
        new_xcconfig = xcconfig + 'OTHER_LDFLAGS[sdk=iphone*] = -framework "Crashlytics" -framework "Fabric"'
        File.open(xcconfig_path, "w") { |file| file << new_xcconfig }
      end
    end
  end
end

然后在 Fabric 的运行脚本构建阶段:

if [[$ARCHS != "x86_64"]]; then
  "${PODS_ROOT}/Fabric/run" [your usual key]
fi
于 2019-11-08T14:50:30.243 回答
13

打开项目 Pods 目录中的 Pods-$projectname.release.xcconfig 文件,并找到 OTHER_LDFLAGS 行。在变量名之后立即添加[sdk=iphone*](例如,我的现在看起来像这样):

OTHER_LDFLAGS[sdk=iphone*] = $(inherited) -ObjC -l"MailCore-ios" -l"c++" -l"iconv" -l"resolv" -l"xml2" -l"z"

这仅在构建 iphone 变体时有条件地设置链接选项,防止 pod 在 OSX 上链接。当然,正如您所提到的,这需要与#if !targetEnvironment(macCatalyst)调用#endifpod 的代码结合使用,否则您将收到链接器错误。

这让我克服了同样的问题。(如果您想知道除了条件变量之外还有哪些很酷的东西可以添加到您的 .xcconfig 文件中,这是我找到的参考:https ://pewpewthespells.com/blog/xcconfig_guide.html )

于 2019-11-07T00:47:49.070 回答
10

我有一个更新的解决方案,适用于以下 Google pod:

  pod 'FirebaseUI/Auth'
  pod 'FirebaseUI/Phone'
  pod 'FirebaseUI/Email'
  pod 'Firebase/Auth'
  pod 'Firebase/Analytics'
  pod 'Fabric', '~> 1.10.2'
  pod 'Firebase/Crashlytics'
  pod 'Firebase/AdMob'
post_install do |installer|
  installer.pods_project.targets.each do |target|
    if target.name.start_with?("Pods")
        puts "Updating #{target.name} to exclude Crashlytics/Fabric"
      target.build_configurations.each do |config|
        xcconfig_path = config.base_configuration_reference.real_path
        xcconfig = File.read(xcconfig_path)
        xcconfig.sub!('-framework "FirebaseAnalytics"', '')
        xcconfig.sub!('-framework "FIRAnalyticsConnector"', '')
        xcconfig.sub!('-framework "GoogleMobileAds"', '')
        xcconfig.sub!('-framework "Google-Mobile-Ads-SDK"', '')
        xcconfig.sub!('-framework "GoogleAppMeasurement"', '')
        xcconfig.sub!('-framework "Fabric"', '')
        new_xcconfig = xcconfig + 'OTHER_LDFLAGS[sdk=iphone*] = $(inherited) -framework "FirebaseAnalytics"  -framework "FIRAnalyticsConnector"  -framework "GoogleMobileAds" -framework "GoogleAppMeasurement" -framework "GoogleUtilities" "-AppMeasurement" -framework "Fabric"'
        File.open(xcconfig_path, "w") { |file| file << new_xcconfig }
      end
    end
  end
end
于 2020-02-13T17:18:24.283 回答
7

使用 cocoapods 1.8.4,我不得不调整@AncAinu 的优秀答案,如下所示:

post_install do |installer|
  installer.pods_project.targets.each do |target|
    if target.name == "Pods-[Name of Project]"
      puts "Updating #{target.name} to exclude Crashlytics/Fabric"
      target.build_configurations.each do |config|
        xcconfig_path = config.base_configuration_reference.real_path
        xcconfig = File.read(xcconfig_path)
        xcconfig.sub!('-framework "Crashlytics"', '')
        xcconfig.sub!('-framework "Fabric"', '')
        new_xcconfig = xcconfig + 'OTHER_LDFLAGS[sdk=iphone*] = -framework "Crashlytics" -framework "Fabric"'
        File.open(xcconfig_path, "w") { |file| file << new_xcconfig }
      end
    end
  end
end
于 2019-12-20T23:07:43.070 回答
3

基于这里已经讨论过的内容......这是我针对具有多个目标的项目的解决方案。它基本上是在验证每个目标上的库的使用情况,而不是遵循目标名称。

post_install do |installer|
    
    installer.pods_project.targets.each do |target|
        
        # handle non catalyst libs
        libs = ["FirebaseAnalytics", "Google-Mobile-Ads-SDK"]
        
        target.build_configurations.each do |config|
            xcconfig_path = config.base_configuration_reference.real_path
            xcconfig = File.read(xcconfig_path)
            values = ""
            
            libs.each { |lib|
                if xcconfig["-framework \"#{lib}\""]
                    puts "Found '#{lib}' on target '#{target.name}'"
                    xcconfig.sub!(" -framework \"#{lib}\"", '')
                    values += " -framework \"#{lib}\""
                end
            }
            
            if values.length > 0
                puts "Preparing '#{target.name}' for Catalyst\n\n"
                new_xcconfig = xcconfig + 'OTHER_LDFLAGS[sdk=iphone*] = $(inherited)' + values
                File.open(xcconfig_path, "w") { |file| file << new_xcconfig }
            end
        end
    end
end


它输出这样的东西

Generating Pods project

Found 'Google-Mobile-Ads-SDK' on target 'Pods-TheApp'
Found 'FirebaseAnalytics' on target 'Pods-TheApp'
Preparing 'Pods-TheApp' for Catalyst

Found 'Google-Mobile-Ads-SDK' on target 'Pods-TheApp-TheAppTests'
Found 'FirebaseAnalytics' on target 'Pods-TheApp-TheAppTests'
Preparing 'Pods-TheApp-TheAppTests' for Catalyst

Found 'Google-Mobile-Ads-SDK' on target 'Pods-TheApp-TheApp_iOS_UI_Tests'
Found 'FirebaseAnalytics' on target 'Pods-TheApp-TheApp_iOS_UI_Tests'
Preparing 'Pods-TheApp-TheApp_iOS_UI_Tests' for Catalyst

Found 'Google-Mobile-Ads-SDK' on target 'Pods-TheAppIntentsExtension'
Found 'FirebaseAnalytics' on target 'Pods-TheAppIntentsExtension'
Preparing 'Pods-TheAppIntentsExtension' for Catalyst

Found 'Google-Mobile-Ads-SDK' on target 'Pods-TheAppTodayExtension'
Found 'FirebaseAnalytics' on target 'Pods-TheAppTodayExtension'
Preparing 'Pods-TheAppTodayExtension' for Catalyst
于 2020-08-24T10:20:46.040 回答