如何在Flutter插件中添加桥接头

时间:2018-10-22 15:04:14

标签: ios xcode flutter cocoapods podspec

我正在尝试创建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 <NearbyMessages/GNSMessages.h>

所以我的问题是,如何在flutter插件中添加桥接头?

1 个答案:

答案 0 :(得分:1)

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

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