arnaucube
arnaucube/openEventsPlatformAppPublic Self-hosted repository
openEventsPlatformApp/plugins/cordova-plugin-device/src/ios/CDVDevice.m
plugins/cordova-plugin-device/src/ios/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/types.h>
 21#include <sys/sysctl.h>
 22#include "TargetConditionals.h"
 23
 24#import <Cordova/CDV.h>
 25#import "CDVDevice.h"
 26
 27@implementation UIDevice (ModelVersion)
 28
 29- (NSString*)modelVersion
 30{
 31    size_t size;
 32
 33    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
 34    char* machine = malloc(size);
 35    sysctlbyname("hw.machine", machine, &size, NULL, 0);
 36    NSString* platform = [NSString stringWithUTF8String:machine];
 37    free(machine);
 38
 39    return platform;
 40}
 41
 42@end
 43
 44@interface CDVDevice () {}
 45@end
 46
 47@implementation CDVDevice
 48
 49- (NSString*)uniqueAppInstanceIdentifier:(UIDevice*)device
 50{
 51    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
 52    static NSString* UUID_KEY = @"CDVUUID";
 53    
 54    // Check user defaults first to maintain backwards compaitibility with previous versions
 55    // which didn't user identifierForVendor
 56    NSString* app_uuid = [userDefaults stringForKey:UUID_KEY];
 57    if (app_uuid == nil) {
 58        if ([device respondsToSelector:@selector(identifierForVendor)]) {
 59            app_uuid = [[device identifierForVendor] UUIDString];
 60        } else {
 61            CFUUIDRef uuid = CFUUIDCreate(NULL);
 62            app_uuid = (__bridge_transfer NSString *)CFUUIDCreateString(NULL, uuid);
 63            CFRelease(uuid);
 64        }
 65
 66        [userDefaults setObject:app_uuid forKey:UUID_KEY];
 67        [userDefaults synchronize];
 68    }
 69    
 70    return app_uuid;
 71}
 72
 73- (void)getDeviceInfo:(CDVInvokedUrlCommand*)command
 74{
 75    NSDictionary* deviceProperties = [self deviceProperties];
 76    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:deviceProperties];
 77
 78    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
 79}
 80
 81- (NSDictionary*)deviceProperties
 82{
 83    UIDevice* device = [UIDevice currentDevice];
 84
 85    return @{
 86             @"manufacturer": @"Apple",
 87             @"model": [device modelVersion],
 88             @"platform": @"iOS",
 89             @"version": [device systemVersion],
 90             @"uuid": [self uniqueAppInstanceIdentifier:device],
 91             @"cordova": [[self class] cordovaVersion],
 92             @"isVirtual": @([self isVirtual])
 93             };
 94}
 95
 96+ (NSString*)cordovaVersion
 97{
 98    return CDV_VERSION;
 99}
100
101- (BOOL)isVirtual
102{
103    #if TARGET_OS_SIMULATOR
104        return true;
105    #elif TARGET_IPHONE_SIMULATOR
106        return true;
107    #else
108        return false;
109    #endif
110}
111
112@end