【问题标题】:How to add bridging header to Flutter Plugin如何将桥接头添加到 Flutter 插件
【发布时间】:2019-03-26 17:03:51
【问题描述】:

我正在尝试创建 Flutter 插件,它使用 Swift 并具有一个 pod 依赖项 (NearbyMessages)

首先我已经添加到.podspec

#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
  s.name             = 'flutter_nearby_messages'
  s.version          = '0.0.1'
  s.summary          = 'A new flutter plugin project.'
  s.description      = <<-DESC
A new flutter plugin project.
                       DESC
  s.homepage         = 'http://example.com'
  s.license          = { :file => '../LICENSE' }
  s.author           = { 'Your Company' => 'email@example.com' }
  s.source           = { :path => '.' }
  s.source_files = 'Classes/**/*'
  s.public_header_files = 'Classes/**/*.h'
  s.dependency 'Flutter'
  //************ I've added this lines ***************
  s.dependency 'NearbyMessages' 
  s.xcconfig = { 'SWIFT_OBJC_BRIDGING_HEADER' => 'Classes/bridging_header.h' }
  //********* ^^ I've added this lines ^^ ************
  s.static_framework = true
  s.ios.deployment_target = '8.0'
end

安装 pod 并构建初始空项目后,一切似乎都很好

我已经尝试在 Objective-C 中使用它:

#import "FlutterNearbyMessagesPlugin.h"
#import <NearbyMessages/GNSMessages.h>
#import <flutter_nearby_messages/flutter_nearby_messages-Swift.h>

@implementation FlutterNearbyMessagesPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {    
    GNSMessageManager *messageManager =
    [[GNSMessageManager alloc] initWithAPIKey:@"API_KEY"];
  [SwiftFlutterNearbyMessagesPlugin registerWithRegistrar:registrar];
}
@end

它有效,但是当我尝试时:

public class SwiftFlutterNearbyMessagesPlugin: NSObject, FlutterPlugin {
    private var client: GNSMessageManager? = nil

XCode 说 Use of undeclared type GNSMessageManager

我了解,我需要桥接头,并且我已经尝试创建它,但它似乎根本没有链接。

桥接头仅包含一行:#import &lt;NearbyMessages/GNSMessages.h&gt;

所以我的问题是,如何将桥接头添加到颤振插件?

【问题讨论】:

  • 可以显示桥接头文件的内容吗?你在文件中导入了 GNSMessageManager 吗?
  • @AndreiDurnea 桥接头仅包含一行#import &lt;NearbyMessages/GNSMessages.h&gt;
  • 你的 Swift 文件中有 import NearbyMessages 吗?
  • 不,我没有。但我知道,每个人都会不时犯愚蠢而简单的错误——我们只是人类。这部分不在您的 Swift 代码示例中,而在您的 Obj-C 代码示例中。我只是想把显而易见的事情排除在外。很抱歉,如果它冒犯了。
  • 为此浪费了 150 个代表赏金,​​太棒了

标签: ios xcode flutter cocoapods podspec


【解决方案1】:

唯一对我有用的将桥接头文件添加到 Flutter 插件的解决方案是将头文件放在 Classes 文件夹中,然后将其添加到 .podspec

s.public_header_files = 'Classes/**/*.h'

【讨论】:

    猜你喜欢
    • 2015-06-24
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    相关资源
    最近更新 更多