Skip to content

Commit

Permalink
Added iOS/Android support for getting carrier information
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoto-Ida committed Nov 9, 2017
1 parent 589633a commit 0a374b5
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 25 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 @@ -14,12 +14,14 @@ @interface RNDeviceInfo()
@property (nonatomic) bool isEmulator;
@end

@import CoreTelephony;

@implementation RNDeviceInfo

@synthesize isEmulator;

RCT_EXPORT_MODULE()

+ (BOOL)requiresMainQueueSetup
{
return YES;
Expand All @@ -31,17 +33,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 @@ -50,7 +52,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 @@ -150,6 +152,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 @@ -204,6 +213,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 @@ -107,6 +107,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 @@ -172,6 +178,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 @@ -28,3 +28,4 @@ export function getIPAddress(): Promise<string>;
export function getMACAddress(): Promise<string>;
export function getPhoneNumber(): string;
export function getAPILevel(): number;
export function getCarrier(): string;
37 changes: 20 additions & 17 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 All @@ -74,16 +74,19 @@ module.exports = {
isTablet: function() {
return RNDeviceInfo.isTablet;
},
isPinOrFingerprintSet: function () {
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 @@ -28,4 +28,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 @@ -27,7 +27,7 @@ public override string Name
}

private bool IsEmulator(string model)
{
{
Regex rgx = new Regex("(?i:virtual)");
return rgx.IsMatch(model);
}
Expand Down Expand Up @@ -83,7 +83,7 @@ private bool IsTablet(string os)
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 @@ -112,6 +112,7 @@ private bool IsTablet(string os)
constants["timezone"] = TimeZoneInfo.Local.Id;
constants["isEmulator"] = IsEmulator(model);
constants["isTablet"] = IsTablet(os);
constants["carrier"] = "not available";

return constants;
}
Expand Down

0 comments on commit 0a374b5

Please sign in to comment.