Skip to content

Commit ae37a75

Browse files
Merged in stage (pull request #248)
MRN-969 Approved-by: vanitha.g
2 parents ca1ada2 + 8aa7b21 commit ae37a75

File tree

12 files changed

+140
-2455
lines changed

12 files changed

+140
-2455
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ yarn.lock
6060
# Ruby / CocoaPods
6161
**/Pods/
6262
/vendor/bundle/
63+
**/Podfile.lock
6364

6465
# Temporary files created by Metro to check the health of the file watcher
6566
.metro-health-check*
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.mirrorfly_rn
2+
3+
import android.annotation.SuppressLint
4+
import android.provider.Settings
5+
import com.facebook.react.bridge.ReactApplicationContext
6+
import com.facebook.react.bridge.ReactContextBaseJavaModule
7+
import com.facebook.react.bridge.Promise
8+
import com.facebook.react.bridge.ReactMethod
9+
10+
class DeviceIdModule(private val reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
11+
override fun getName(): String = "DeviceId"
12+
13+
@SuppressLint("HardwareIds")
14+
@ReactMethod
15+
fun getDeviceID(promise: Promise) {
16+
try {
17+
val androidId = Settings.Secure.getString(
18+
reactContext.contentResolver,
19+
Settings.Secure.ANDROID_ID
20+
)
21+
promise.resolve(androidId)
22+
} catch (e: Exception) {
23+
promise.reject("DEVICE_ID_ERROR", "Failed to get Android ID", e)
24+
}
25+
}
26+
}

android/app/src/main/java/com/mirrorfly_rn/MFModule.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class MFModule : ReactPackage {
1414
modules.add(BluetoothHeadsetDetectModule(reactContext))
1515
modules.add(RingtoneSilentKeyEventModule(reactContext))
1616
modules.add(MediaService(reactContext))
17+
modules.add(DeviceIdModule(reactContext))
1718
return modules
1819
}
1920

ios/DeviceIdModule.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// DeviceIdModule.m
3+
// mirrorfly_rn
4+
//
5+
// Created by user on 24/04/25.
6+
7+
// DeviceIdModule.m
8+
#import <Foundation/Foundation.h>
9+
#import <React/RCTBridgeModule.h>
10+
11+
12+
@interface RCT_EXTERN_MODULE(DeviceId, NSObject)
13+
RCT_EXTERN_METHOD(getDeviceID:(RCTPromiseResolveBlock *)resolve
14+
rejecter:(RCTPromiseRejectBlock)reject)
15+
@end
16+

ios/DeviceIdModule.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// DeviceIdModule.swift
3+
// mirrorfly_rn
4+
//
5+
// Created by user on 24/04/25.
6+
//
7+
import React
8+
import Foundation
9+
import UIKit
10+
import Security
11+
12+
@objc(DeviceId)
13+
class DeviceId: NSObject {
14+
let key = "com.mirrorfly.deviceid"
15+
@objc
16+
func getDeviceID(
17+
_ resolve: @escaping RCTPromiseResolveBlock,
18+
rejecter reject: @escaping RCTPromiseRejectBlock
19+
) {
20+
if let data = KeychainHelper.load(key: key),
21+
let existingUUID = String(data: data, encoding: .utf8) {
22+
resolve(existingUUID)
23+
} else {
24+
let uuid = UUID().uuidString.replacingOccurrences(of: "-", with: "")
25+
let shortened = String(uuid.prefix(8))
26+
if KeychainHelper.save(key: key, data: shortened.data(using: .utf8)!) {
27+
resolve(shortened)
28+
} else {
29+
reject("DEVICE_ID_ERROR", "Failed to save UUID to Keychain", nil)
30+
}
31+
}
32+
}
33+
}
34+
35+
36+
class KeychainHelper {
37+
38+
static func save(key: String, data: Data) -> Bool {
39+
let query = [
40+
kSecClass: kSecClassGenericPassword,
41+
kSecAttrAccount: key,
42+
kSecValueData: data
43+
] as CFDictionary
44+
45+
SecItemDelete(query) // delete existing
46+
let status = SecItemAdd(query, nil)
47+
return status == errSecSuccess
48+
}
49+
50+
static func load(key: String) -> Data? {
51+
let query = [
52+
kSecClass: kSecClassGenericPassword,
53+
kSecAttrAccount: key,
54+
kSecReturnData: kCFBooleanTrue!,
55+
kSecMatchLimit: kSecMatchLimitOne
56+
] as CFDictionary
57+
58+
var dataTypeRef: AnyObject?
59+
let status = SecItemCopyMatching(query, &dataTypeRef)
60+
if status == errSecSuccess {
61+
return dataTypeRef as? Data
62+
}
63+
return nil
64+
}
65+
}
66+

0 commit comments

Comments
 (0)