Dmytro Rostopira
Dmytro Rostopira

Reputation: 11185

How to add bridging header to Flutter Plugin

I'm trying to create Flutter plugin, which uses Swift and has one pod dependency (NearbyMessages)

First of all, I've added it to .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 protected]' }
  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

After pod install and initial empty project built, everything seems to be fine

I've tried using it in 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

And it works, but when I try:

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

XCode says Use of undeclared type GNSMessageManager

I understand, that I need bridging header, and I've already tried to creating it, but it seems not to be linked at all.

Bridging header contains just one line: #import <NearbyMessages/GNSMessages.h>

So my question is, how to add bridging header to flutter plugin?

Upvotes: 16

Views: 4417

Answers (1)

HemOdd
HemOdd

Reputation: 767

The only solution that worked for me for adding a Bridging Header file to a Flutter plugin was putting the header file in Classes folder and then adding this to .podspec:

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

Upvotes: 8

Related Questions