1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-10 14:58:34 +00:00
Files
samples/experimental/pedometer/src/pedometerHelper.m
Liam Appelbe f0e6da6d24 Simplify experimental pedometer example (#2104)
Since ffigen added support for
[`NativeCallable.listener`](https://api.flutter.dev/flutter/dart-ffi/NativeCallable/NativeCallable.listener.html)
to its ObjC bindings, this example can be simplified. We can replace the
`Dart_Port` logic with `ObjCBlock.listener`, which lets us get rid of
most of the native code.

We still need a small bit of native code to `retain` a reference to the
callback's arguments before invoking the listener, otherwise the
arguments may be ref counted and deleted before the Dart side of the
callback is invoked. See https://github.com/dart-lang/native/issues/835
2023-12-06 18:58:58 +10:00

13 lines
410 B
Objective-C

#import "pedometerHelper.h"
#import <CoreMotion/CoreMotion.h>
#import <CoreMotion/CMPedometer.h>
#import <Foundation/Foundation.h>
// TODO(https://github.com/dart-lang/native/issues/835): Generate this wrapper
// automatically.
CMPedometerHandler wrapCallback(CMPedometerHandler callback) {
return [^(CMPedometerData *data, NSError *error) {
return callback([data retain], [error retain]);
} copy];
}