Skip to content

How to reuse identical MacOS and iOS code in a plugin #52285

@ryanheise

Description

@ryanheise

In some cases it is possible to add MacOS support to a plugin by simply copying ios/Classes/*.m to macos/Classes/*.m and making some small changes to the headers and podspec (as demonstrated by this PR: ryanheise/just_audio#54). As @sachaarbonel (the author of that PR) wrote:

This this how sqflite did it and there are no best practices instructions from the flutter team so until then ...

If it were possible to reuse the code, that would make maintenance a lot easier when new features are added.

In that example, within the *.h files, the only difference is:

#import <FlutterMacOS/FlutterMacOS.h>

vs

#import <Flutter/Flutter.h>

But the *.m code is identical and works on both platforms.

Given that iOS and MacOS do largely offer the same set of APIs, I think this use case could be quite common, but I do not see any Flutter documentation or examples on how to reuse an identical iOS/MacOS implementation.

The documentation (https://flutter.dev/desktop#add-desktop-support-to-an-existing-flutter-project) refers to the following official plugin examples as a guide: url_launcher. shared_preferences, connectivity, path_provider. However, these don't show how to reuse code that is identical. On the contrary, those examples actually implement the iOS side in Objective C, and the MacOS side in Swift, so there is definitely no code reuse.

Metadata

Metadata

Labels

P3Issues that are less important to the Flutter projectpackageflutter/packages repository. See also p: labels.platform-iosiOS applications specificallyplatform-macBuilding on or for macOS specificallytoolAffects the "flutter" command-line tool. See also t: labels.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions