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@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

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?

Dmytro Rostopira
  • 10,588
  • 4
  • 64
  • 86
  • Can you show the contents of the bridging header file? Have you imported GNSMessageManager in the file? – Durdu Oct 30 '18 at 07:29
  • @AndreiDurnea bridging header contains just one line `#import ` – Dmytro Rostopira Oct 30 '18 at 07:42
  • Did you `import NearbyMessages` in your Swift file? – Losiowaty Oct 30 '18 at 09:53
  • 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. – Losiowaty Oct 30 '18 at 09:58
  • Wasted 150 rep bounty on this, great – Dmytro Rostopira Nov 06 '18 at 09:57
  • I am interesed and knowing how to do this also, you found any solution? – Daniel Oliveira Jan 24 '19 at 18:40
  • @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 https://github.com/rostopira/flutter_nearby_messages and this file https://github.com/rostopira/flutter_nearby_messages/blob/master/ios/Classes/NearbyMessages/module.modulemap – Dmytro Rostopira Jan 25 '19 at 07:43
  • @DanielOliveira problem is actually in dependency pod, they don't have `.modulemap`. If you can contact developers of required dependency, ask them to add it – Dmytro Rostopira Jan 25 '19 at 07:44

1 Answers1

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'
HemOdd
  • 697
  • 1
  • 7
  • 23