openEventsPlatformApp/plugins/cordova-plugin-device/src/osx/CDVDevice.m
plugins/cordova-plugin-device/src/osx/CDVDevice.mRaw
1/*
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18 */
19
20#include <sys/sysctl.h>
21
22#import "CDVDevice.h"
23
24#define SYSTEM_VERSION_PLIST @"/System/Library/CoreServices/SystemVersion.plist"
25
26@implementation CDVDevice
27
28- (NSString*) modelVersion {
29 size_t size;
30
31 sysctlbyname("hw.machine", NULL, &size, NULL, 0);
32 char* machine = malloc(size);
33 sysctlbyname("hw.machine", machine, &size, NULL, 0);
34 NSString* modelVersion = [NSString stringWithUTF8String:machine];
35 free(machine);
36
37 return modelVersion;
38}
39
40
41- (NSString*) getSerialNr {
42 NSString* serialNr;
43 io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
44 if (platformExpert) {
45 CFTypeRef serialNumberAsCFString =
46 IORegistryEntryCreateCFProperty(platformExpert,
47 CFSTR(kIOPlatformSerialNumberKey),
48 kCFAllocatorDefault, 0);
49 if (serialNumberAsCFString) {
50 serialNr = (__bridge NSString*) serialNumberAsCFString;
51 }
52 IOObjectRelease(platformExpert);
53 }
54 return serialNr;
55}
56
57- (NSString*) uniqueAppInstanceIdentifier {
58 NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
59 static NSString* UUID_KEY = @"CDVUUID";
60
61 NSString* app_uuid = [userDefaults stringForKey:UUID_KEY];
62
63 if (app_uuid == nil) {
64 CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
65 CFStringRef uuidString = CFUUIDCreateString(kCFAllocatorDefault, uuidRef);
66
67 app_uuid = [NSString stringWithString:(__bridge NSString*) uuidString];
68 [userDefaults setObject:app_uuid forKey:UUID_KEY];
69 [userDefaults synchronize];
70
71 CFRelease(uuidString);
72 CFRelease(uuidRef);
73 }
74
75 return app_uuid;
76}
77
78- (NSString*) platform {
79 return [NSDictionary dictionaryWithContentsOfFile:SYSTEM_VERSION_PLIST][@"ProductName"];
80}
81
82- (NSString*) systemVersion {
83 return [NSDictionary dictionaryWithContentsOfFile:SYSTEM_VERSION_PLIST][@"ProductVersion"];
84}
85
86- (void) getDeviceInfo:(CDVInvokedUrlCommand*) command {
87 NSDictionary* deviceProperties = [self deviceProperties];
88 CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:deviceProperties];
89
90 [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
91}
92
93- (NSDictionary*) deviceProperties {
94 NSMutableDictionary* devProps = [NSMutableDictionary dictionaryWithCapacity:4];
95
96 devProps[@"manufacturer"] = @"Apple";
97 devProps[@"model"] = [self modelVersion];
98 devProps[@"platform"] = [self platform];
99 devProps[@"version"] = [self systemVersion];
100 devProps[@"uuid"] = [self uniqueAppInstanceIdentifier];
101 devProps[@"cordova"] = [[self class] cordovaVersion];
102 devProps[@"serial"] = [self getSerialNr];
103 devProps[@"isVirtual"] = @NO;
104
105 NSDictionary* devReturn = [NSDictionary dictionaryWithDictionary:devProps];
106 return devReturn;
107}
108
109+ (NSString*) cordovaVersion {
110 return CDV_VERSION;
111}
112
113@end