diff --git a/README.md b/README.md index f737c2a7e..80d05fc04 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ var DeviceInfo = require('react-native-device-info'); | Serial Number | `getSerialNumber()` | `string` | Only supported in Android | IP Address | `getIPAddress()` | `Promise` | Only supported in Android | MAC Address | `getMACAddress()` | `Promise` | 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 diff --git a/RNDeviceInfo/RNDeviceInfo.m b/RNDeviceInfo/RNDeviceInfo.m index eed1b5f99..e8042fce8 100644 --- a/RNDeviceInfo/RNDeviceInfo.m +++ b/RNDeviceInfo/RNDeviceInfo.m @@ -16,12 +16,14 @@ @interface RNDeviceInfo() @property (nonatomic) bool isEmulator; @end +@import CoreTelephony; + @implementation RNDeviceInfo @synthesize isEmulator; RCT_EXPORT_MODULE() - + + (BOOL)requiresMainQueueSetup { return YES; @@ -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; } @@ -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) @@ -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 @@ -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), diff --git a/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java b/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java index 916d6c428..a936bad5d 100644 --- a/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java +++ b/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java @@ -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 getConstants() { HashMap constants = new HashMap(); @@ -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; } } diff --git a/deviceinfo.d.ts b/deviceinfo.d.ts index cdbf41139..41d0b652a 100644 --- a/deviceinfo.d.ts +++ b/deviceinfo.d.ts @@ -29,3 +29,4 @@ export function getIPAddress(): Promise; export function getMACAddress(): Promise; export function getPhoneNumber(): string; export function getAPILevel(): number; +export function getCarrier(): string; diff --git a/deviceinfo.js b/deviceinfo.js index ed8f82e0f..d558dcc34 100644 --- a/deviceinfo.js +++ b/deviceinfo.js @@ -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() { @@ -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; @@ -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; + }, }; diff --git a/deviceinfo.js.flow b/deviceinfo.js.flow index d87d75514..8b276dc4d 100644 --- a/deviceinfo.js.flow +++ b/deviceinfo.js.flow @@ -29,4 +29,5 @@ declare module.exports: { getSerialNumber: () => string, getIPAddress: () => Promise, getMACAddress: () => Promise, + getCarrier: () => string, } diff --git a/windows/RNDeviceInfo/RNDeviceInfoModule.cs b/windows/RNDeviceInfo/RNDeviceInfoModule.cs index 9ed72442c..60b378c16 100644 --- a/windows/RNDeviceInfo/RNDeviceInfoModule.cs +++ b/windows/RNDeviceInfo/RNDeviceInfoModule.cs @@ -26,7 +26,7 @@ public override string Name } private bool IsEmulator(string model) - { + { Regex rgx = new Regex("(?i:virtual)"); return rgx.IsMatch(model); } @@ -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); @@ -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;