Skip to content

Commit

Permalink
Merge pull request #261 from Naoto-Ida/master
Browse files Browse the repository at this point in the history
Added iOS/Android support for getting carrier information
  • Loading branch information
GantMan committed Nov 24, 2017
2 parents 098c379 + 444f5a7 commit 5b50d6a
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -193,7 +193,7 @@ var DeviceInfo = require('react-native-device-info');
| Serial Number | `getSerialNumber()` | `string` | Only supported in Android
| IP Address | `getIPAddress()` | `Promise<string>` | Only supported in Android
| MAC Address | `getMACAddress()` | `Promise<string>` | Only supported in Android

| Carrier | `getCarrier()` | `string` e.g. "SOFTBANK" | | |
Since the device setting for PIN/Fingerprint can be modified while the app is still open, this is available via callback instead of as a constant. To use, pass a callback function to the returned bridge function in your javascript:

```js
Expand Down
20 changes: 15 additions & 5 deletions RNDeviceInfo/RNDeviceInfo.m
Expand Up @@ -16,12 +16,14 @@ @interface RNDeviceInfo()
@property (nonatomic) bool isEmulator;
@end

@import CoreTelephony;

@implementation RNDeviceInfo

@synthesize isEmulator;

RCT_EXPORT_MODULE()

+ (BOOL)requiresMainQueueSetup
{
return YES;
Expand All @@ -33,17 +35,17 @@ - (NSString*) deviceId
struct utsname systemInfo;

uname(&systemInfo);

NSString* deviceId = [NSString stringWithCString:systemInfo.machine
encoding:NSUTF8StringEncoding];

if ([deviceId isEqualToString:@"i386"] || [deviceId isEqualToString:@"x86_64"] ) {
deviceId = [NSString stringWithFormat:@"%s", getenv("SIMULATOR_MODEL_IDENTIFIER")];
self.isEmulator = YES;
} else {
self.isEmulator = NO;
}

return deviceId;
}

Expand All @@ -52,7 +54,7 @@ - (NSString*) deviceName
static NSDictionary* deviceNamesByCode = nil;

if (!deviceNamesByCode) {

deviceNamesByCode = @{@"iPod1,1" :@"iPod Touch", // (Original)
@"iPod2,1" :@"iPod Touch", // (Second Generation)
@"iPod3,1" :@"iPod Touch", // (Third Generation)
Expand Down Expand Up @@ -152,6 +154,13 @@ - (NSString*) deviceName
return deviceName;
}

- (NSString *) carrier
{
CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [netinfo subscriberCellularProvider];
return carrier.carrierName;
}

- (NSString*) userAgent
{
#if TARGET_OS_TV
Expand Down Expand Up @@ -212,6 +221,7 @@ - (NSDictionary *)constantsToExport
@"appVersion": [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] ?: [NSNull null],
@"buildNumber": [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"],
@"systemManufacturer": @"Apple",
@"carrier": self.carrier ?: [NSNull null],
@"userAgent": self.userAgent,
@"timezone": self.timezone,
@"isEmulator": @(self.isEmulator),
Expand Down
Expand Up @@ -111,6 +111,12 @@ public void getMacAddress(Promise p) {
p.resolve(macAddress);
}

@ReactMethod
public String getCarrier() {
TelephonyManager telMgr = (TelephonyManager) this.reactContext.getSystemService(Context.TELEPHONY_SERVICE);
return telMgr.getNetworkOperatorName();
}

@Override
public @Nullable Map<String, Object> getConstants() {
HashMap<String, Object> constants = new HashMap<String, Object>();
Expand Down Expand Up @@ -177,6 +183,7 @@ public void getMacAddress(Promise p) {
TelephonyManager telMgr = (TelephonyManager) this.reactContext.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
constants.put("phoneNumber", telMgr.getLine1Number());
}
constants.put("carrier", this.getCarrier());
return constants;
}
}
1 change: 1 addition & 0 deletions deviceinfo.d.ts
Expand Up @@ -29,3 +29,4 @@ export function getIPAddress(): Promise<string>;
export function getMACAddress(): Promise<string>;
export function getPhoneNumber(): string;
export function getAPILevel(): number;
export function getCarrier(): string;
35 changes: 19 additions & 16 deletions deviceinfo.js
Expand Up @@ -5,40 +5,40 @@
var RNDeviceInfo = require('react-native').NativeModules.RNDeviceInfo;

module.exports = {
getUniqueID: function () {
getUniqueID: function() {
return RNDeviceInfo.uniqueId;
},
getInstanceID: function() {
return RNDeviceInfo.instanceId;
},
getSerialNumber: function () {
getSerialNumber: function() {
return RNDeviceInfo.serialNumber;
},
getIPAddress: function () {
getIPAddress: function() {
return RNDeviceInfo.getIpAddress();
},
getMACAddress: function () {
getMACAddress: function() {
return RNDeviceInfo.getMacAddress();
},
getDeviceId: function () {
getDeviceId: function() {
return RNDeviceInfo.deviceId;
},
getManufacturer: function () {
getManufacturer: function() {
return RNDeviceInfo.systemManufacturer;
},
getModel: function () {
getModel: function() {
return RNDeviceInfo.model;
},
getBrand: function () {
getBrand: function() {
return RNDeviceInfo.brand;
},
getSystemName: function () {
getSystemName: function() {
return RNDeviceInfo.systemName;
},
getSystemVersion: function () {
getSystemVersion: function() {
return RNDeviceInfo.systemVersion;
},
getAPILevel: function () {
getAPILevel: function() {
return RNDeviceInfo.apiLevel;
},
getBundleId: function() {
Expand All @@ -51,7 +51,7 @@ module.exports = {
return RNDeviceInfo.appVersion;
},
getReadableVersion: function() {
return RNDeviceInfo.appVersion + "." + RNDeviceInfo.buildNumber;
return RNDeviceInfo.appVersion + '.' + RNDeviceInfo.buildNumber;
},
getDeviceName: function() {
return RNDeviceInfo.deviceName;
Expand Down Expand Up @@ -80,13 +80,16 @@ module.exports = {
isPinOrFingerprintSet: function () {
return RNDeviceInfo.isPinOrFingerprintSet;
},
getFirstInstallTime: function () {
getFirstInstallTime: function() {
return RNDeviceInfo.firstInstallTime;
},
getLastUpdateTime: function () {
getLastUpdateTime: function() {
return RNDeviceInfo.lastUpdateTime;
},
getPhoneNumber: function () {
getPhoneNumber: function() {
return RNDeviceInfo.phoneNumber;
}
},
getCarrier: function() {
return RNDeviceInfo.carrier;
},
};
1 change: 1 addition & 0 deletions deviceinfo.js.flow
Expand Up @@ -29,4 +29,5 @@ declare module.exports: {
getSerialNumber: () => string,
getIPAddress: () => Promise<string>,
getMACAddress: () => Promise<string>,
getCarrier: () => string,
}
5 changes: 3 additions & 2 deletions windows/RNDeviceInfo/RNDeviceInfoModule.cs
Expand Up @@ -26,7 +26,7 @@ public override string Name
}

private bool IsEmulator(string model)
{
{
Regex rgx = new Regex("(?i:virtual)");
return rgx.IsMatch(model);
}
Expand Down Expand Up @@ -87,7 +87,7 @@ private bool is24Hour()
model = deviceInfo.SystemProductName;
hardwareVersion = deviceInfo.SystemHardwareVersion;
os = deviceInfo.OperatingSystem;


string deviceFamilyVersion = Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamilyVersion;
ulong version2 = ulong.Parse(deviceFamilyVersion);
Expand Down Expand Up @@ -116,6 +116,7 @@ private bool is24Hour()
constants["timezone"] = TimeZoneInfo.Local.Id;
constants["isEmulator"] = IsEmulator(model);
constants["isTablet"] = IsTablet(os);
constants["carrier"] = "not available";
constants["is24Hour"] = is24Hour();

return constants;
Expand Down

0 comments on commit 5b50d6a

Please sign in to comment.