cupertino_ffi 0.1.6 cupertino_ffi: ^0.1.6 copied to clipboard
A large number of APIs by Apple made available with 'dart:ffi'. Contains helpers for using any Objective-C library.
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 #
- github.com/dart-interop/cupertino_ffi
- 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 asPointer<NSString>
and vice-versa.
Generated libraries #
Dartdoc can be found here.
Name | Documentation | Import | Description |
---|---|---|---|
CloudKit | docs | "package:cupertino_ffi/cloudkit.dart" | Cloud-based storage. |
Contacts | docs | "package:cupertino_ffi/contacts.dart" | Contacts. |
Core Data | docs | "package:cupertino_ffi/core_data.dart" | Loading and storing data. |
Core Graphics | docs | "package:cupertino_ffi/core_graphics.dart" | Images. |
Core Location | docs | "package:cupertino_ffi/core_location.dart" | Geographical location. |
Core ML | docs | "package:cupertino_ffi/core_ml.dart" | Machine learning. |
Core Spotlight | docs | "package:cupertino_ffi/core_spotlight.dart" | Search. |
Core WLAN | docs | "package:cupertino_ffi/core_wlan.dart" | WLAN. |
EventKit | docs | "package:cupertino_ffi/eventkit.dart" | Calendar events. |
Foundation | docs | "package:cupertino_ffi/foundation.dart" | Essential APIs. |
HomeKit | docs | "package:cupertino_ffi/homekit.dart" | Home automation. |
Multipeer Connectivity | docs | "package:cupertino_ffi/multipeer_connectivity.dart" | Peer-to-peer connectivity. |
ModelIO | docs | "package:cupertino_ffi/modelio.dart" | 3D model assets. |
Natural Language | docs | "package:cupertino_ffi/natural_language.dart" | Natural language processing (NLP). |
Objective-C runtime | docs | "package:cupertino_ffi/objective_c.dart" | Objective-C internals. |
PassKit | docs | "package:cupertino_ffi/passkit.dart" | Apple Pay and Apple Wallet. |
PreferencePanes | docs | "package:cupertino_ffi/preferencepanes.dart" | System preferences. |
SceneKit | docs | "package:cupertino_ffi/scenekit.dart" | 3D rendering. |
Security | docs | "package:cupertino_ffi/security.dart" | Keychain, cryptography, authentication. |
Speech | docs | "package:cupertino_ffi/speech.dart" | Speech recognition. |
Social | docs | "package:cupertino_ffi/social.dart" | Social media. |
StoreKit | docs | "package:cupertino_ffi/storekit.dart" | App Store. |
Vision | docs | "package:cupertino_ffi/vision.dart" | Computer vision. |
WebKit | docs | "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() {
generateAll(ObjcBinding(
libraries: libraries,
packageName: "cupertino_ffi",
));
}
final libraries = [
ObjcLibraryBinding(
dynamicLibrary: DynamicLibraryInfo(
path: "/System/Library/Frameworks/CloudKit.framework/Versions/A/CloudKit",
name: "CloudKit",
url: "https://developer.apple.com/documentation/cloudkit?language=objc",
),
name: "cloudkit",
),
ObjcLibraryBinding(
dynamicLibrary: DynamicLibraryInfo(
path: "/System/Library/Frameworks/CoreData.framework/Versions/A/CoreData",
name: "Core Data",
url: "https://developer.apple.com/documentation/coredata?language=objc",
),
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.
arcPush();
try {
final 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);
}
Generating Objective-C bindings yourself #
import 'package:ffi_tool/objective_c.dart';
void main() {
generateAll([
ObjcLibrary(
// Just for documentation purposes
productName: "Core ML",
uri: "https://developer.apple.com/documentation/coreml",
// For generated source code
libraryName: "example",
libraryPath: "/System/Library/Frameworks/CoreML.framework/Versions/Current/CoreML",
generatedPath: "core_ml.dart",
),
]);
}