Skip to content

Commit 18dc5d9

Browse files
authored
Implement support for getting utsname info on iOS (flutter#239)
* Implement support for getting utsname info on iOS * Fix typo * Add dartdoc and update version no * Add missing newline * Review feedback * Move utsname wrapping to IosUtsname class * Fix caps per name convention * Fix format * Review feedback * Complete merge
1 parent 64745cc commit 18dc5d9

File tree

6 files changed

+66
-4
lines changed

6 files changed

+66
-4
lines changed

packages/device_info/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
## 0.0.2 - Oct 20, 2017
1+
## 0.0.3 - October 24, 2017
2+
3+
* Add support for utsname
4+
5+
## 0.0.2 - October 20, 2017
26

37
* Fixed broke type comparison
48
* Added "isPhysicaldevice" field, detecting emulators/simulators

packages/device_info/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
1818
print('Running on ${androidInfo.model}'); // e.g. "Moto G (4)"
1919
2020
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
21-
print('Running on ${iosInfo.model}'); // e.g. "iPhone 6"
21+
print('Running on ${iosInfo.utsname.machine}'); // e.g. "iPod7,1"
2222
```
2323

2424
You will find links to the API docs on the [pub page](https://pub.dartlang.org/packages/device_info).

packages/device_info/example/lib/main.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ class _MyAppState extends State<MyApp> {
9494
'localizedModel': data.localizedModel,
9595
'identifierForVendor': data.identifierForVendor,
9696
'isPhysicalDevice': data.isPhysicalDevice,
97+
'utsname.sysname:': data.utsname.sysname,
98+
'utsname.nodename:': data.utsname.nodename,
99+
'utsname.release:': data.utsname.release,
100+
'utsname.version:': data.utsname.version,
101+
'utsname.machine:': data.utsname.machine,
97102
};
98103
}
99104

packages/device_info/ios/Classes/DeviceInfoPlugin.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
#import "DeviceInfoPlugin.h"
6+
#import <sys/utsname.h>
67

78
@implementation DeviceInfoPlugin
89
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
@@ -16,6 +17,8 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
1617
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
1718
if ([@"getIosDeviceInfo" isEqualToString:call.method]) {
1819
UIDevice* device = [UIDevice currentDevice];
20+
struct utsname un;
21+
uname(&un);
1922

2023
result(@{
2124
@"name" : [device name],
@@ -25,6 +28,13 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
2528
@"localizedModel" : [device localizedModel],
2629
@"identifierForVendor" : [[device identifierForVendor] UUIDString],
2730
@"isPhysicalDevice" : [self isDevicePhysical],
31+
@"utsname" : @{
32+
@"sysname" : @(un.sysname),
33+
@"nodename" : @(un.nodename),
34+
@"release" : @(un.release),
35+
@"version" : @(un.version),
36+
@"machine" : @(un.machine),
37+
}
2838
});
2939
} else {
3040
result(FlutterMethodNotImplemented);

packages/device_info/lib/device_info.dart

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ class IosDeviceInfo {
208208
this.localizedModel,
209209
this.identifierForVendor,
210210
this.isPhysicalDevice,
211+
this.utsname,
211212
});
212213

213214
/// Device name.
@@ -231,8 +232,11 @@ class IosDeviceInfo {
231232
/// `false` if the application is running in a simulator, `true` otherwise.
232233
final bool isPhysicalDevice;
233234

235+
/// Operating system information derived from `sys/utsname.h`.
236+
final IosUtsname utsname;
237+
234238
/// Deserializes from the JSON message received from [_kChannel].
235-
static IosDeviceInfo _fromJson(Map<String, Object> json) {
239+
static IosDeviceInfo _fromJson(Map<String, dynamic> json) {
236240
return new IosDeviceInfo._(
237241
name: json['name'],
238242
systemName: json['systemName'],
@@ -241,6 +245,45 @@ class IosDeviceInfo {
241245
localizedModel: json['localizedModel'],
242246
identifierForVendor: json['identifierForVendor'],
243247
isPhysicalDevice: json['isPhysicalDevice'] == 'true',
248+
utsname: IosUtsname._fromJson(json['utsname']),
249+
);
250+
}
251+
}
252+
253+
/// Information derived from `utsname`.
254+
/// See http://pubs.opengroup.org/onlinepubs/7908799/xsh/sysutsname.h.html for details.
255+
class IosUtsname {
256+
IosUtsname._({
257+
this.sysname,
258+
this.nodename,
259+
this.release,
260+
this.version,
261+
this.machine,
262+
});
263+
264+
/// Operating system name.
265+
final String sysname;
266+
267+
/// Network node name.
268+
final String nodename;
269+
270+
/// Release level.
271+
final String release;
272+
273+
/// Version level.
274+
final String version;
275+
276+
/// Hardware type (e.g. 'iPhone7,1' for iPhone 6 Plus).
277+
final String machine;
278+
279+
/// Deserializes from the JSON message received from [_kChannel].
280+
static IosUtsname _fromJson(Map<String, dynamic> json) {
281+
return new IosUtsname._(
282+
sysname: json['sysname'],
283+
nodename: json['nodename'],
284+
release: json['release'],
285+
version: json['version'],
286+
machine: json['machine'],
244287
);
245288
}
246289
}

packages/device_info/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: device_info
22
description: Provides detailed information about the device the Flutter app is running on.
3-
version: 0.0.2
3+
version: 0.0.3
44
author: Flutter Team <flutter-dev@googlegroups.com>
55
homepage: https://github.com/flutter/plugins/tree/master/packages/device_info
66

0 commit comments

Comments
 (0)