cupertino_ffi 0.1.5

  • Readme
  • Changelog
  • Example
  • Installing
  • new64

Pub Package

Overview #

This package enables Dart developers to use a large number Objective-C APIs. The package uses dart:ffi and the 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.

Github project #

  • Github project
  • The project appreciates bug reports, suggestions, and general feedback.

Core foundation #

  • Documentation: developer.apple.com
  • 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.

NameDocumentationImportDescription
CloudKitdocs"package:cupertino_ffi/cloudkit.dart"Cloud-based storage.
Contactsdocs"package:cupertino_ffi/contacts.dart"Contacts.
Core Datadocs"package:cupertino_ffi/core_data.dart"Loading and storing data.
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.
Multipeer Connectivitydocs"package:cupertino_ffi/multipeer_connectivity.dart"Peer-to-peer connectivity.
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.
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!

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.
    arcPush();
    try {
      Pointer pointer = CFDictionary.fromDart({"key": "value"});
      return "some return value";
    } finally {
      // Pop the topmost ARC frame.
      arcPop();
    }
}

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').

Example:

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

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);
}

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

example/example.dart

import 'dart:ffi';

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

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

    // 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 = CFError.toDart(errorPtrPtr.load<Pointer<CFError>>());
    if (error != null) {
      throw error;
    }

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

Use this package as a library

1. Depend on it

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


dependencies:
  cupertino_ffi: ^0.1.5

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_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/natural_language.dart';
import 'package:cupertino_ffi/objective_c.dart';
import 'package:cupertino_ffi/objective_c_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/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';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
31
Health:
Code health derived from static analysis. [more]
94
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
64
Learn more about scoring.

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

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in libraries.

Health issues and suggestions

Document public APIs. (-0.87 points)

38540 out of 38704 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/objective_c_helpers.dart. (-1 points)

Analysis of lib/objective_c_helpers.dart reported 2 hints:

line 24 col 8: Unused import: 'dart:convert'.

line 26 col 8: Unused import: 'dart:typed_data'.

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

Analysis of lib/contacts.dart reported 1 hint:

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

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

Analysis of lib/core_ml.dart reported 1 hint:

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

Fix additional 6 files with analysis or formatting issues. (-3 points)

Additional issues in the following files:

  • lib/core_wlan.dart (1 hint)
  • lib/homekit.dart (1 hint)
  • lib/natural_language.dart (1 hint)
  • lib/passkit.dart (1 hint)
  • lib/safari_services.dart (1 hint)
  • lib/storekit.dart (1 hint)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.5.0 <3.0.0
ffi ^0.1.1 0.1.2 0.1.3-dev.3
meta ^1.1.0 1.1.7
Dev dependencies
ffi_tool any
pedantic ^1.8.0
test ^1.6.0