使用 CMake 将 Swift 导入 Objective-C

问题描述 投票:0回答:1

我有一个带有 CMake 的 QT 应用程序。 CMake 生成

.xcodeproj
CocoaPods 从以下 .xcworkspace
 生成 
Podfile

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, :deployment_target => '14.0'

target 'MyApp' do
  pod 'YandexMobileAdsAdMobAdapters'
  pod 'YandexMobileAdsIronSourceAdapters'
  pod 'YandexMobileAdsMediation'
  pod 'YandexMobileAdsInstream'
end

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

pod install
命令生成以下标头:

./Pods/YandexMobileAdsIronSourceAdapters/YandexMobileAdsIronSourceAdapters.xcframework/ios-arm64_x86_64-simulator/YandexMobileAdsIronSourceAdapters.framework/Headers/YandexMobileAdsIronSourceAdapters-umbrella.h
./Pods/YandexMobileAdsIronSourceAdapters/YandexMobileAdsIronSourceAdapters.xcframework/ios-arm64_x86_64-simulator/YandexMobileAdsIronSourceAdapters.framework/Headers/YandexMobileAdsIronSourceAdapters-Swift.h
./Pods/YandexMobileAdsIronSourceAdapters/YandexMobileAdsIronSourceAdapters.xcframework/ios-arm64/YandexMobileAdsIronSourceAdapters.framework/Headers/YandexMobileAdsIronSourceAdapters-umbrella.h
./Pods/YandexMobileAdsIronSourceAdapters/YandexMobileAdsIronSourceAdapters.xcframework/ios-arm64/YandexMobileAdsIronSourceAdapters.framework/Headers/YandexMobileAdsIronSourceAdapters-Swift.h
./Pods/YandexMobileAds/static/YandexMobileAds.xcframework/ios-arm64_x86_64-simulator/YandexMobileAds.framework/Headers/YandexMobileAds-umbrella.h
./Pods/YandexMobileAds/static/YandexMobileAds.xcframework/ios-arm64_x86_64-simulator/YandexMobileAds.framework/Headers/YandexMobileAds-Swift.h
./Pods/YandexMobileAds/static/YandexMobileAds.xcframework/ios-arm64_x86_64-simulator/YandexMobileAds.framework/Headers/YandexMobileAds.h
./Pods/YandexMobileAds/static/YandexMobileAds.xcframework/ios-arm64/YandexMobileAds.framework/Headers/YandexMobileAds-umbrella.h
./Pods/YandexMobileAds/static/YandexMobileAds.xcframework/ios-arm64/YandexMobileAds.framework/Headers/YandexMobileAds-Swift.h
./Pods/YandexMobileAds/static/YandexMobileAds.xcframework/ios-arm64/YandexMobileAds.framework/Headers/YandexMobileAds.h
./Pods/YandexMobileAdsInstream/YandexMobileAdsInstream.xcframework/ios-arm64_x86_64-simulator/YandexMobileAdsInstream.framework/Headers/YandexMobileAdsInstreamVersion.h
./Pods/YandexMobileAdsInstream/YandexMobileAdsInstream.xcframework/ios-arm64_x86_64-simulator/YandexMobileAdsInstream.framework/Headers/YandexMobileAdsInstream-Swift.h
./Pods/YandexMobileAdsInstream/YandexMobileAdsInstream.xcframework/ios-arm64/YandexMobileAdsInstream.framework/Headers/YandexMobileAdsInstreamVersion.h
./Pods/YandexMobileAdsInstream/YandexMobileAdsInstream.xcframework/ios-arm64/YandexMobileAdsInstream.framework/Headers/YandexMobileAdsInstream-Swift.h
./Pods/YandexMobileAdsAdMobAdapters/YandexMobileAdsAdMobAdapters.xcframework/ios-arm64_x86_64-simulator/YandexMobileAdsAdMobAdapters.framework/Headers/YandexMobileAdsAdMobAdapters-Swift.h
./Pods/YandexMobileAdsAdMobAdapters/YandexMobileAdsAdMobAdapters.xcframework/ios-arm64_x86_64-simulator/YandexMobileAdsAdMobAdapters.framework/Headers/YandexMobileAdsAdMobAdapters-umbrella.h
./Pods/YandexMobileAdsAdMobAdapters/YandexMobileAdsAdMobAdapters.xcframework/ios-arm64/YandexMobileAdsAdMobAdapters.framework/Headers/YandexMobileAdsAdMobAdapters-Swift.h
./Pods/YandexMobileAdsAdMobAdapters/YandexMobileAdsAdMobAdapters.xcframework/ios-arm64/YandexMobileAdsAdMobAdapters.framework/Headers/YandexMobileAdsAdMobAdapters-umbrella.h

但我无法将它们包含到我的项目中的

.mm
文件中。我尝试了各种路径,例如

#import <YandexMobileAds.h>
#import <YandexMobileAds/YandexMobileAds-Swift.h>
#import <YandexMobileAds/YandexMobileAds.h>

但没有成功:

enter image description here

  1. 为什么没有被发现?

我的第一个想法是以某种方式公开它们,但我没有在

Header
页面上找到
Build Phases
部分:

enter image description here

(像

Generate .rcc/qmlcache .. 
这样的任务有很多,但不是
Headers
)。

enter image description here

XCode 显示了以下 Pod:

enter image description here

  1. 另一个问题是为什么不生成
    YandexMobileAdsMediation
    header? (但仅限 YandexMobileAds)

请参阅 Yandex 文档,了解如何在 Swift 项目中使用其包。

  1. 是否可以使用 CMake 将 Swift 包添加到项目中? (可以链接到 Swift 库,但是 Swift 包呢?)

编辑1:

我不太了解 Objective-C 语法,但至少我能够在生成的

InterstitialAdLoader
标头中找到所需的类名
YandexMobileAds-Swift.h
,并且我可以假设它是在这里定义的:

grep -ri "InterstitialAdLoader" . --include=\*.h

./YandexMobileAds-Swift.h:@protocol YMAInterstitialAdLoaderDelegate;
./YandexMobileAds-Swift.h:SWIFT_CLASS_NAMED("InterstitialAdLoader")
./YandexMobileAds-Swift.h:@interface YMAInterstitialAdLoader : NSObject
./YandexMobileAds-Swift.h:@property (nonatomic, weak) id <YMAInterstitialAdLoaderDelegate> _Nullable delegate;
./YandexMobileAds-Swift.h:SWIFT_PROTOCOL_NAMED("InterstitialAdLoaderDelegate")
./YandexMobileAds-Swift.h:@protocol YMAInterstitialAdLoaderDelegate <NSObject>
./YandexMobileAds-Swift.h:/// \param adLoader A reference to an object of the <code>InterstitialAdLoader</code> class that invoked the method.
./YandexMobileAds-Swift.h:- (void)interstitialAdLoader:(YMAInterstitialAdLoader * _Nonnull)adLoader didLoad:(YMAInterstitialAd * _Nonnull)interstitialAd;
./YandexMobileAds-Swift.h:/// \param adLoader A reference to an object of the <code>InterstitialAdLoader</code> class that invoked the method.
./YandexMobileAds-Swift.h:- (void)interstitialAdLoader:(YMAInterstitialAdLoader * _Nonnull)adLoader didFailToLoadWithError:(YMAAdRequestError * _Nonnull)error;

因此理论上将

YandexMobileAds-Swift.h
包含到我的
.mm
文件中可以朝着正确的方向迈进。

swift objective-c cocoapods yandex
1个回答
0
投票

我用这个更改了 Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, :deployment_target => '14.0'

target 'LinesGameQt' do
  pod 'YandexMobileAdsMediation', '7.5.1'
end

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

并且能够使用

 编译我的 
.mm

文件
#import <YandexMobileAds/YandexMobileAds-Swift.h>
© www.soinside.com 2019 - 2024. All rights reserved.