如何将桥接头添加到 Flutter 插件
How to add bridging header to Flutter Plugin
我正在尝试创建 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 插件?
将桥接头文件添加到 Flutter 插件对我有用的唯一解决方案是将头文件放在 Classes
文件夹中,然后将其添加到 .podspec
:
s.public_header_files = 'Classes/**/*.h'
我正在尝试创建 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 插件?
将桥接头文件添加到 Flutter 插件对我有用的唯一解决方案是将头文件放在 Classes
文件夹中,然后将其添加到 .podspec
:
s.public_header_files = 'Classes/**/*.h'