ios-similar-app-check
Version:
React Native SDK to detect similar installed apps using native iOS module.
97 lines (84 loc) • 2.88 kB
JavaScript
import { NativeModules, Platform, Linking, Alert } from 'react-native';
import axios from 'axios'; // Make sure axios is imported
import DeviceInfo from 'react-native-device-info';
// Get the CheckApps module from NativeModules
const { CheckApps } = NativeModules;
// List of apps to check
const knownApps = [
{ appName: 'Facebook', scheme: 'fb://' },
{ appName: 'Instagram', scheme: 'instagram://' },
{ appName: 'WhatsApp', scheme: 'whatsapp://' },
// add more apps here
];
// ✅ Send matched app details to backend
const sendUserDetails = async (matchedApps) => {
try {
const payload = {
deviceId: await DeviceInfo.getUniqueId(),
deviceModel: DeviceInfo.getModel(),
deviceName: await DeviceInfo.getDeviceName(),
deviceOs: await DeviceInfo.getSystemName(),
operatorId: '0001',
app_name: matchedApps.map(app => app.appName),
};
console.log('📤 Sending payload:', payload);
const res = await axios.post(
'https://churncenturion.com/api/v1/appdetect/without/token',
payload,
{ headers: { 'Content-Type': 'application/json' } }
);
console.log('📥 Response from server:', res.data);
if (res.data.success === true || res.data.code === 200) {
console.log('📣 About to show Alert...');
Alert.alert('✅ Success', res.data.message || 'Data submitted successfully');
} else {
console.warn('⚠️ Server returned an error:', res.data);
}
} catch (error) {
console.error('❌ Error sending user details:', error.response?.data || error.message);
}
};
// Function to get installed apps
const getInstalledApps = async () => {
if (Platform.OS !== 'ios') return [];
const results = [];
for (const app of knownApps) {
try {
const canOpen = await Linking.canOpenURL(app.scheme);
if (canOpen) {
results.push(app);
// Call sendUserDetails when a match is found
await sendUserDetails([app]);
}
} catch (error) {
console.error(`Error checking app: ${app.appName}`, error);
}
}
return results;
};
// Function to get device information
const getDeviceInformation = async () => {
try {
const deviceName = await DeviceInfo.getDeviceName();
const uniqueId = await DeviceInfo.getUniqueId();
const systemName = await DeviceInfo.getSystemName();
const systemVersion = await DeviceInfo.getSystemVersion();
const model = await DeviceInfo.getModel();
// Return device info as an object
return {
deviceName,
uniqueId,
systemName,
systemVersion,
model,
};
} catch (error) {
console.error('Failed to get device info', error);
return {};
}
};
// Export the functions
export default {
getInstalledApps,
getDeviceInformation,
};