Skip to content

feat: One module for iOS and MacOS instead of two #42

@rekire

Description

@rekire

Migrated from VeryGoodOpenSource/very_good_flutter_plugin#154

Description

There is a way to declare that you want to use the same code for iOS and MacOS. There was a proposal which explained it quite well. This is already in use e.g. for the path_provider package. Which this feature you can avoid a lot of duplicated code (I expect that you do the same on both platforms).

Requirements

  • Rewrite the plugin to support that

Additional Context

Here is an example how the MyPlugin.swift should look like afterwards:

#if os(iOS)
import Flutter
#elseif os(macOS)
import FlutterMacOS
#endif

public class MyPlugin: NSObject, FlutterPlugin {
    public static func register(with registrar: FlutterPluginRegistrar) {
        // Workaround for https://github.com/flutter/flutter/issues/118103.
#if os(iOS)
        let messenger = registrar.messenger()
#else
        let messenger = registrar.messenger
#endif
        let channel = FlutterMethodChannel(
            name: "my_plugin_darwin",
            binaryMessenger: messenger)
        let instance = MyPlugin()
        registrar.addMethodCallDelegate(instance, channel: channel)
    }

//...

The podfile should look like this:

Pod::Spec.new do |s|
  s.name             = 'my_plugin_darwin'
  s.version          = '0.0.1'
  s.summary          = 'The iOS and MacOS implementation of my plugin.'
  s.description      = <<-DESC
  The iOS and MacOS implementation of the my plugin.
                       DESC
  s.homepage         = 'http://example.com'
  s.license          = { :type => 'BSD', :file => '../LICENSE' }
  s.author           = { 'Your Company' => '[email protected]' }
  s.source           = { :path => '.' }
  s.source_files = 'Classes/**/*'

  s.swift_version = '5.0'

  s.ios.dependency 'Flutter'
  s.osx.dependency 'FlutterMacOS'
  s.ios.deployment_target = '11.0' # or what ever version you need
  s.osx.deployment_target = '11.0' # or what ever version you need
end

The my_plugin_darwin/pubspec.yaml like this:

flutter:
  plugin:
    implements: my_plugin
    platforms:
      ios:
        pluginClass: MyPlugin
        dartPluginClass: MyPluginDarwin
        sharedDarwinSource: true
      macos:
        pluginClass: MyPlugin
        dartPluginClass: MyPluginDarwin
        sharedDarwinSource: true

Those are of cause not all changes, but that should reflect the most relevant.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Needs Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions