Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ios): detect if app is running on a macOS desktop with Apple Silicon #167

Merged
merged 5 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,16 @@ var string = device.serial;
### Android Quirk

As of Android 9, the underlying native API that powered the `uuid` property is deprecated and will always return `UNKNOWN` without proper permissions. Cordova have never implemented handling the required permissions. As of Android 10, **all** non-resettable device identifiers are no longer readable by normal applications and will always return `UNKNOWN`. More information can be [read here](https://developer.android.com/about/versions/10/privacy/changes#non-resettable-device-ids).

## device.isiOSAppOnMac

The iOS app is running on the Mac desktop (Apple Silicon ARM64 processor, M1 or newer).
This parameter is only returned for iOS V14.0 or later, and is not returned for Android devices.

```js
var boolean = device.isiOSAppOnMac;
```

### Supported Platforms

- iOS
21 changes: 18 additions & 3 deletions src/ios/CDVDevice.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Licensed to the Apache Software Foundation (ASF) under one
#include <sys/sysctl.h>
#include "TargetConditionals.h"

#import <Availability.h>

#import <Cordova/CDV.h>
#import "CDVDevice.h"

Expand Down Expand Up @@ -53,7 +55,7 @@ - (NSString*)uniqueAppInstanceIdentifier:(UIDevice*)device
{
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
static NSString* UUID_KEY = @"CDVUUID";

// Check user defaults first to maintain backwards compaitibility with previous versions
// which didn't user identifierForVendor
NSString* app_uuid = [userDefaults stringForKey:UUID_KEY];
Expand All @@ -69,7 +71,7 @@ - (NSString*)uniqueAppInstanceIdentifier:(UIDevice*)device
[userDefaults setObject:app_uuid forKey:UUID_KEY];
[userDefaults synchronize];
}

return app_uuid;
}

Expand All @@ -92,7 +94,8 @@ - (NSDictionary*)deviceProperties
@"version": [device systemVersion],
@"uuid": [self uniqueAppInstanceIdentifier:device],
@"cordova": [[self class] cordovaVersion],
@"isVirtual": @([self isVirtual])
@"isVirtual": @([self isVirtual]),
@"isiOSAppOnMac": @([self isiOSAppOnMac])
};
}

Expand All @@ -112,4 +115,16 @@ - (BOOL)isVirtual
#endif
}


- (BOOL) isiOSAppOnMac
{
#if __IPHONE_14_0
if (@available(iOS 14.0, *)) {
return [[NSProcessInfo processInfo] isiOSAppOnMac];
}
#endif

return false;
}

@end
15 changes: 14 additions & 1 deletion src/osx/CDVDevice.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Licensed to the Apache Software Foundation (ASF) under one

#include <sys/sysctl.h>

#import <Availability.h>

#import "CDVDevice.h"

#define SYSTEM_VERSION_PLIST @"/System/Library/CoreServices/SystemVersion.plist"
Expand All @@ -37,7 +39,6 @@ - (NSString*) modelVersion {
return modelVersion;
}


- (NSString*) getSerialNr {
NSString* serialNr;
io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
Expand Down Expand Up @@ -101,6 +102,7 @@ - (NSDictionary*) deviceProperties {
devProps[@"cordova"] = [[self class] cordovaVersion];
devProps[@"serial"] = [self getSerialNr];
devProps[@"isVirtual"] = @NO;
devProps[@"isiOSAppOnMac"]: [self isiOSAppOnMac];

NSDictionary* devReturn = [NSDictionary dictionaryWithDictionary:devProps];
return devReturn;
Expand All @@ -110,4 +112,15 @@ + (NSString*) cordovaVersion {
return CDV_VERSION;
}

- (BOOL) isiOSAppOnMac
{
#if __IPHONE_14_0
if (@available(iOS 14.0, *)) {
return [[NSProcessInfo processInfo] isiOSAppOnMac];
}
#endif

return false;
}

@end
5 changes: 5 additions & 0 deletions www/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function Device () {
this.manufacturer = null;
this.isVirtual = null;
this.serial = null;
this.isiOSAppOnMac = null;

var me = this;

Expand All @@ -59,6 +60,10 @@ function Device () {
me.cordova = buildLabel;
me.model = info.model;
me.isVirtual = info.isVirtual;
// isiOSAppOnMac is iOS specific. If defined, it will be appended.
if (info.isiOSAppOnMac !== undefined) {
me.isiOSAppOnMac = info.isiOSAppOnMac;
}
me.manufacturer = info.manufacturer || 'unknown';
me.serial = info.serial || 'unknown';

Expand Down