cupertino_ffi 0.1.6

  • Readme
  • Changelog
  • Example
  • Installing
  • 69

Pub Package

Overview #

This package enables Dart developers to use a large number Objective-C APIs. The package uses dart:ffi and the C APIs are generated with ffi_tool.

Most Flutter developers should not use this package. It's usually a better idea to write a Flutter plugin than use this package. Flutter plugins are less likely to contain memory management bugs, they are automatically isolated from the UI event loop, they support all APIs, and the development experience is just a lot better.

The advantages of this package are automatic generation of APIs (no need to write message passing code) and support for non-Flutter applications.

If you decide to use this package, you must follow the correct reference counting patterns. The patterns are documented below.

Contributing #

Core foundation #

  • Documentation:
  • Import "package:cupertino_ffi/core_foundation.dart"
  • Note that Foundation types (NSString, etc.) and Core Foundation types (CFString, etc.) are "toll-free" bridged types. This means that Pointer<CFString> can be used as Pointer<NSString> and vice-versa.

Generated libraries #

Dartdoc can be found here.

CloudKitdocs"package:cupertino_ffi/cloudkit.dart"Cloud-based storage.
Core Datadocs"package:cupertino_ffi/core_data.dart"Loading and storing data.
Core Graphicsdocs"package:cupertino_ffi/core_graphics.dart"Images.
Core Locationdocs"package:cupertino_ffi/core_location.dart"Geographical location.
Core MLdocs"package:cupertino_ffi/core_ml.dart"Machine learning.
Core Spotlightdocs"package:cupertino_ffi/core_spotlight.dart"Search.
Core WLANdocs"package:cupertino_ffi/core_wlan.dart"WLAN.
EventKitdocs"package:cupertino_ffi/eventkit.dart"Calendar events.
Foundationdocs"package:cupertino_ffi/foundation.dart"Essential APIs.
HomeKitdocs"package:cupertino_ffi/homekit.dart"Home automation.
Multipeer Connectivitydocs"package:cupertino_ffi/multipeer_connectivity.dart"Peer-to-peer connectivity.
ModelIOdocs"package:cupertino_ffi/modelio.dart"3D model assets.
Natural Languagedocs"package:cupertino_ffi/natural_language.dart"Natural language processing (NLP).
Objective-C runtimedocs"package:cupertino_ffi/objective_c.dart"Objective-C internals.
PassKitdocs"package:cupertino_ffi/passkit.dart"Apple Pay and Apple Wallet.
PreferencePanesdocs"package:cupertino_ffi/preferencepanes.dart"System preferences.
SceneKitdocs"package:cupertino_ffi/scenekit.dart"3D rendering.
Securitydocs"package:cupertino_ffi/security.dart"Keychain, cryptography, authentication.
Speechdocs"package:cupertino_ffi/speech.dart"Speech recognition.
Socialdocs"package:cupertino_ffi/social.dart"Social media.
StoreKitdocs"package:cupertino_ffi/storekit.dart"App Store.
Visiondocs"package:cupertino_ffi/vision.dart"Computer vision.
WebKitdocs"package:cupertino_ffi/webkit.dart"Browser engine.

Want to add a library? Create an issue!

Generating bindings for an Objective-C library #

Objective-C supports reflection so we are able to generate APIs automatically. The generated libraries use cupertino_ffi for reference counting. Please make sure you understand the reference counting patterns that you need to follow when you use the generated library.

For example, our generator script looks like this:

import 'package:cupertino_ffi/objc_ffi_generator.dart';

void main() {
    libraries: libraries,
    packageName: "cupertino_ffi",

final libraries = [
    dynamicLibrary: DynamicLibraryInfo(
      path: "/System/Library/Frameworks/CloudKit.framework/Versions/A/CloudKit",
      name: "CloudKit",
      url: "",
    name: "cloudkit",

    dynamicLibrary: DynamicLibraryInfo(
      path: "/System/Library/Frameworks/CoreData.framework/Versions/A/CoreData",
      name: "Core Data",
      url: "",
    name: "core_data",

  // ...

Memory management patterns #

Calling APIs that return ARC pointers #

If you call any API that returns a reference-counter pointer, you need to call arcPush and arcPop like in the following example:

String example() {
    // Push a new ARC frame.
    try {
      final pointer = CFDictionary.fromDart({"key": "value"});
      return "some return value";
    } finally {
      // Pop the topmost ARC frame.

Returning ARC pointers #

If you return a pointer to reference counted value, you must call function 'arcReturn', which inserts the pointer into caller's reference counting frame and increments the reference count (so it will 2 before 'arcPop').


Pointer<CFDictionary> example() {
    try {
      final result = CFDictionary.fromDart({"key": "value"});
      return arcReturn(result);
    } finally {

Storing ARC pointer #

You need to ensure that arcFieldGet is invoked in the getter and arcFieldSet is invoked in the setter.

import 'package:cupertino_ffi/core_foundation.dart';

Pointer<CFString> _field;

Pointer<CFString> get field => arcFieldGet(_field);

set field(Pointer<CFString> newValue) {
  _field = arcFieldSet(_field, newValue);

Generating Objective-C bindings yourself #

import 'package:ffi_tool/objective_c.dart';

void main() {
      // Just for documentation purposes
      productName: "Core ML",
      uri: "",

      // For generated source code
      libraryName: "example",
      libraryPath: "/System/Library/Frameworks/CoreML.framework/Versions/Current/CoreML",
      generatedPath: "core_ml.dart",

0.1.6 #

  • Support for Dart 2.6 and extension classes.

0.1.5 #

  • Improved documentation.

0.1.4 #

  • A large number of fixes.

0.1.3 #

  • A large number of fixes.

0.1.2 #

  • Fixed various issues.

0.1.1 #

  • Fixed suggestions by the package analyzer.

0.1.0 #

  • Initial release


import 'dart:ffi';
import 'package:ffi/ffi.dart';

import 'package:cupertino_ffi/core_foundation.dart';
import 'package:cupertino_ffi/security.dart';

void main() {
  try {
    // Allocate an error variable on heap.
    final errorPtrPtr = allocate<Pointer<CFError>>();

    // Define RSA key attributes
    final attributes = CFDictionary.fromPointerMap({
      kSecAttrKeyType: kSecAttrKeyTypeRSA,
      kSecAttrKeySizeInBits: CFNumber.fromDart(2048),

    // Generate a random key.
    final secKey = SecKeyCreateRandomKey(attributes, errorPtrPtr);

    // Check for errors
    final error = errorPtrPtr.value.toDart();
    if (error != null) {
      throw error;

    // Get public key
    final publicKey = SecKeyCopyPublicKey(secKey);
  } finally {

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  cupertino_ffi: ^0.1.6

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:cupertino_ffi/cloudkit.dart';
import 'package:cupertino_ffi/contacts.dart';
import 'package:cupertino_ffi/core_data.dart';
import 'package:cupertino_ffi/core_foundation.dart';
import 'package:cupertino_ffi/core_graphics.dart';
import 'package:cupertino_ffi/core_location.dart';
import 'package:cupertino_ffi/core_ml.dart';
import 'package:cupertino_ffi/core_spotlight.dart';
import 'package:cupertino_ffi/core_wlan.dart';
import 'package:cupertino_ffi/eventkit.dart';
import 'package:cupertino_ffi/foundation.dart';
import 'package:cupertino_ffi/homekit.dart';
import 'package:cupertino_ffi/modelio.dart';
import 'package:cupertino_ffi/natural_language.dart';
import 'package:cupertino_ffi/objc.dart';
import 'package:cupertino_ffi/objc_ffi_generator.dart';
import 'package:cupertino_ffi/objc_helpers.dart';
import 'package:cupertino_ffi/passkit.dart';
import 'package:cupertino_ffi/preferencepanes.dart';
import 'package:cupertino_ffi/safari_services.dart';
import 'package:cupertino_ffi/scenekit.dart';
import 'package:cupertino_ffi/security.dart';
import 'package:cupertino_ffi/social.dart';
import 'package:cupertino_ffi/speech.dart';
import 'package:cupertino_ffi/storekit.dart';
import 'package:cupertino_ffi/vision.dart';
import 'package:cupertino_ffi/webkit.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jan 16, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Health suggestions

Fix lib/src/generated/core_graphics/generated.dart. (-1 points)

Analysis of lib/src/generated/core_graphics/generated.dart reported 2 hints:

line 5 col 8: Unused import: 'package:ffi/ffi.dart'.

line 6 col 8: Unused import: 'package:cupertino_ffi/objc.dart'.

Fix lib/src/objc/classes.dart. (-1 points)

Analysis of lib/src/objc/classes.dart reported 2 hints:

line 23 col 8: Unused import: 'package:ffi/ffi.dart'.

line 25 col 8: Unused import: 'package:cupertino_ffi/objc.dart'.

Fix lib/cloudkit.dart. (-0.50 points)

Analysis of lib/cloudkit.dart reported 1 hint:

line 9 col 8: Unused import: 'package:ffi/ffi.dart'.

Fix additional 25 files with analysis or formatting issues. (-12 points)

Additional issues in the following files:

  • lib/contacts.dart (1 hint)
  • lib/core_data.dart (1 hint)
  • lib/core_location.dart (1 hint)
  • lib/core_ml.dart (1 hint)
  • lib/core_spotlight.dart (1 hint)
  • lib/core_wlan.dart (1 hint)
  • lib/eventkit.dart (1 hint)
  • lib/foundation.dart (1 hint)
  • lib/homekit.dart (1 hint)
  • lib/modelio.dart (1 hint)
  • lib/natural_language.dart (1 hint)
  • lib/passkit.dart (1 hint)
  • lib/preferencepanes.dart (1 hint)
  • lib/safari_services.dart (1 hint)
  • lib/scenekit.dart (1 hint)
  • lib/social.dart (1 hint)
  • lib/speech.dart (1 hint)
  • lib/src/generated/core_foundation/generated.dart (1 hint)
  • lib/src/generated/security/generated.dart (1 hint)
  • lib/src/objc/runtime.dart (1 hint)
  • lib/src/objc_ffi_generator/objc_dispatcher_generator.dart (1 hint)
  • lib/storekit.dart (1 hint)
  • lib/vision.dart (1 hint)
  • lib/webkit.dart (1 hint)
  • lib/src/core_foundation/array.dart (Run dartfmt to format lib/src/core_foundation/array.dart.)


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.1 <3.0.0
ffi ^0.1.3 0.1.3
meta ^1.1.0 1.1.8
Dev dependencies
ffi_tool ^0.1.2
pedantic ^1.8.0
test ^1.6.0