|
1 | 1 | # ui_device |
2 | 2 |
|
3 | | -A new Flutter FFI plugin project. |
| 3 | +A Flutter plugin for accessing UIDevice information on iOS. |
4 | 4 |
|
5 | | -## Getting Started |
| 5 | +## Usage |
6 | 6 |
|
7 | | -This project is a starting point for a Flutter |
8 | | -[FFI plugin](https://docs.flutter.dev/development/platform-integration/c-interop), |
9 | | -a specialized package that includes native code directly invoked with Dart FFI. |
10 | | - |
11 | | -## Project stucture |
12 | | - |
13 | | -This template uses the following structure: |
14 | | - |
15 | | -* `src`: Contains the native source code, and a CmakeFile.txt file for building |
16 | | - that source code into a dynamic library. |
17 | | - |
18 | | -* `lib`: Contains the Dart code that defines the API of the plugin, and which |
19 | | - calls into the native code using `dart:ffi`. |
20 | | - |
21 | | -* platform folders (`android`, `ios`, `windows`, etc.): Contains the build files |
22 | | - for building and bundling the native code library with the platform application. |
23 | | - |
24 | | -## Buidling and bundling native code |
25 | | - |
26 | | -The `pubspec.yaml` specifies FFI plugins as follows: |
27 | | - |
28 | | -```yaml |
29 | | - plugin: |
30 | | - platforms: |
31 | | - some_platform: |
32 | | - ffiPlugin: true |
33 | | -``` |
34 | | -
|
35 | | -This configuration invokes the native build for the various target platforms |
36 | | -and bundles the binaries in Flutter applications using these FFI plugins. |
37 | | -
|
38 | | -This can be combined with dartPluginClass, such as when FFI is used for the |
39 | | -implementation of one platform in a federated plugin: |
| 7 | +To use this plugin, add `ui_device` as a [dependency in your pubspec.yaml file](https://flutter.dev/docs/development/packages-and-plugins/using-packages). |
40 | 8 |
|
41 | 9 | ```yaml |
42 | | - plugin: |
43 | | - implements: some_other_plugin |
44 | | - platforms: |
45 | | - some_platform: |
46 | | - dartPluginClass: SomeClass |
47 | | - ffiPlugin: true |
| 10 | +dependencies: |
| 11 | + ui_device: |
| 12 | + git: |
| 13 | + url: https://github.com/MixinNetwork/flutter-plugins.git |
| 14 | + path: packages/ui_device |
48 | 15 | ``` |
49 | 16 |
|
50 | | -A plugin can have both FFI and method channels: |
| 17 | +Then import the package: |
51 | 18 |
|
52 | | -```yaml |
53 | | - plugin: |
54 | | - platforms: |
55 | | - some_platform: |
56 | | - pluginClass: SomeName |
57 | | - ffiPlugin: true |
| 19 | +```dart |
| 20 | +import 'package:ui_device/ui_device.dart' as ui_device; |
58 | 21 | ``` |
59 | 22 |
|
60 | | -The native build systems that are invoked by FFI (and method channel) plugins are: |
61 | | -
|
62 | | -* For Android: Gradle, which invokes the Android NDK for native builds. |
63 | | - * See the documentation in android/build.gradle. |
64 | | -* For iOS and MacOS: Xcode, via CocoaPods. |
65 | | - * See the documentation in ios/ui_device.podspec. |
66 | | - * See the documentation in macos/ui_device.podspec. |
67 | | -* For Linux and Windows: CMake. |
68 | | - * See the documentation in linux/CMakeLists.txt. |
69 | | - * See the documentation in windows/CMakeLists.txt. |
70 | | -
|
71 | | -## Binding to native code |
| 23 | +### Getting Device Information |
72 | 24 |
|
73 | | -To use the native code, bindings in Dart are needed. |
74 | | -To avoid writing these by hand, they are generated from the header file |
75 | | -(`src/ui_device.h`) by `package:ffigen`. |
76 | | -Regenerate the bindings by running `flutter pub run ffigen --config ffigen.yaml`. |
| 25 | +`ui_device` provides a single method `current` which returns a `DeviceInfo` object containing various properties of the device. |
77 | 26 |
|
78 | | -## Invoking native code |
79 | | - |
80 | | -Very short-running native functions can be directly invoked from any isolate. |
81 | | -For example, see `sum` in `lib/ui_device.dart`. |
82 | | - |
83 | | -Longer-running functions should be invoked on a helper isolate to avoid |
84 | | -dropping frames in Flutter applications. |
85 | | -For example, see `sumAsync` in `lib/ui_device.dart`. |
86 | | - |
87 | | -## Flutter help |
| 27 | +```dart |
| 28 | +final current = ui_device.current; |
| 29 | +print(current.systemName); // e.g. "iOS" |
| 30 | +print(current.systemVersion); // e.g. "14.4.1" |
| 31 | +print(current.name); // e.g. "iPhone XS Max" |
| 32 | +print(current.model); // e.g. "iPhone11,6" |
| 33 | +``` |
88 | 34 |
|
89 | | -For help getting started with Flutter, view our |
90 | | -[online documentation](https://flutter.dev/docs), which offers tutorials, |
91 | | -samples, guidance on mobile development, and a full API reference. |
| 35 | +You can also get additional information such as the `localizedModel`, `identifierForVendor`, and `isPhysicalDevice`. |
92 | 36 |
|
| 37 | +```dart |
| 38 | +final current = ui_device.current; |
| 39 | +print(current.localizedModel); // e.g. "iPhone" |
| 40 | +print(current.identifierForVendor); // a unique identifier for a device, persisted across app installs |
| 41 | +``` |
0 commit comments