Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interop: Objective C & Swift #49673

Open
2 of 3 tasks
mit-mit opened this issue Aug 16, 2022 · 4 comments
Open
2 of 3 tasks

Interop: Objective C & Swift #49673

mit-mit opened this issue Aug 16, 2022 · 4 comments
Assignees
Labels
area-vm Use area-vm for VM related issues, including code coverage, FFI, and the AOT and JIT backends. type-enhancement A request for a change that isn't a bug

Comments

@mit-mit
Copy link
Member

mit-mit commented Aug 16, 2022

Dart currently supports interop with C code via dart:ffi. We should investigate if we can provide similar interop with Objective C and Swift code.

This would be a supplement to Flutter's plugins, and would allow for calling into Objective C / Swift directly from Dart code, also from pure-Dart apps (such as CLI apps).

  • Objective C interop, binary and source code: Has experimental support, see the example
  • Swift interop, with source code access: Has experimental support, see the example
  • Swift interop, binary/without source code access:
    TODO

If you have any feedback and suggestions about this project, please comment below.

@mit-mit mit-mit added area-vm Use area-vm for VM related issues, including code coverage, FFI, and the AOT and JIT backends. type-enhancement A request for a change that isn't a bug labels Aug 16, 2022
@mit-mit mit-mit modified the milestones: Dart 3 alpha, Dart 3 stable Sep 7, 2022
@fotiDim
Copy link

fotiDim commented Jul 31, 2023

I would be interested in trying out the Swift interop, binary/without source code access since our use case is consuming a native .xcframework written in Swift, in a Flutter plugin. We do have the source code of the .xcframework so I am not sure if it possible to use the Swift interop, with source code access method and then swap out the code with the .xcframework.

For now we are using platform channels and we are thinking of skipping Pigeon completely if FFIgen will be soon the way to go.

@a-siva
Copy link
Contributor

a-siva commented Jul 31, 2023

//cc @liamappelbe

@dowobeha
Copy link

dowobeha commented Nov 5, 2023

I would be interested in this functionality, for Swift specifically.

@liamappelbe
Copy link
Contributor

I've looked into this a bit and I don't think it's possible to interop directly with a swift binary without some sort of intermediary layer. The only way I've seen for interop with swift is to:

  1. Annotate your API with @objc (if you don't own the library, write a wrapper library and mark that with the annotation).
  2. Use the swift compiler to generate an ObjC wrapper header (a 1 liner swiftc command)
  3. ObjC obeys the C calling convention, so we can do interop. In Dart's case, that means using ffigen to parse the ObjC header then using FFI to load the library.

If you don't have access to the source code, just a compiled swift library, then you can still do step 1 by writing a wrapper swift library that you annotate with @objc. You'll end up with 2 libraries to link into your app: the original library and the wrapper library.

There's also a C interop annotation, @_cdecl, but it's undocumented and unofficial, and wouldn't actually simplify things.

We're also considering ways of automating steps 1 and 2, but that won't fundamentally change this workflow.

If anyone knows a cleaner way of interoping with swift, I'd love to hear about it, but afaik this is the only option atm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, FFI, and the AOT and JIT backends. type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants