最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

flutter - Problem when dart interop with swift, using ffigen - Stack Overflow

programmeradmin8浏览0评论

I am trying to make a flutter desktop application that tracks down app usage and anize them.

For this, it is crucial to get active working window name that user is using. I founded how to achieve active window name capturing on windows environment using win32 package, but I cannot find anything equivalent in macOS.

My thought was to use native swift code and use ffigen for dart interop.

This is the code and ffigen settings that I have used, and tested it.

import AppKit

@objc(ApplicationManager)
@objcMembers
public class ApplicationManager: NSObject {
    public override init() {
        super.init()
    }
    
    public func getRunningApplications() -> [String] {
        let runningApps = NSWorkspace.shared.runningApplications
        return runningAppspactMap { $0.localizedName }
    }
}
swiftc -c RunningApplicationsProvider.swift \
  -module-name RunningApplicationsModule \
  -emit-objc-header-path RunningApplicationsProvider.h \
  -emit-library -o libRunningApplicationsProvider.dylib

To generate required Object-C entrypoint for dart interop

ffigen:
  name: RunningApplicationsModule
  description: Bindings for RunningApplicationsProvider.
  language: objc
  output: 'lib/src/generated/running_apps_bindings.dart'
  exclude-all-by-default: true
  objc-interfaces:
    include:
      - 'ApplicationManager'
  headers:
    entry-points:
      - 'ios/native/RunningApplicationsProvider.h'

ffigen yaml config

And finally, actual code that I have tested upon.

import 'dart:ffi';
import 'package:opened_application/src/generated/running_apps_bindings.dart';

void main() {
  // No DynamicLibrary, as it isnt' used.
  final appManager = ApplicationManager.new1();

  final runningApps = appManager.getRunningApplications();

  print(runningApps);
}

I did this based on /interop/objective-c-interop#swift-example this example, but it keeps throwing error that it cannot create a class on the dart side.

发布评论

评论列表(0)

  1. 暂无评论