How to add bridging header to Flutter Plugin
Asked Answered
T

1

16

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?

Thrifty answered 22/10, 2018 at 15:4 Comment(8)
Can you show the contents of the bridging header file? Have you imported GNSMessageManager in the file?Chiquia
@AndreiDurnea bridging header contains just one line #import <NearbyMessages/GNSMessages.h>Thrifty
Did you import NearbyMessages in your Swift file?Upshot
No, I do not. I know though, that everyone makes stupid and simple mistakes from time to time - we are only human. And that part was not in your Swift code sample, while it was in your Obj-C code sample. I was just trying to get the obvious out of the way. I'm sorry if it came through as offending.Upshot
Wasted 150 rep bounty on this, greatThrifty
I am interesed and knowing how to do this also, you found any solution?Abercromby
@DanielOliveira yeah, I've found pretty hacky solution, it works locally but I'm afraid to publish it to pub. Take a look at this repo github.com/rostopira/flutter_nearby_messages and this file github.com/rostopira/flutter_nearby_messages/blob/master/ios/…Thrifty
@DanielOliveira problem is actually in dependency pod, they don't have .modulemap. If you can contact developers of required dependency, ask them to add itThrifty
B
7

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'
Breakage answered 17/2, 2020 at 12:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.