Chapter 10 of 12 ~15 min read

Device Info, Geolocation & Network

Device info, GPS positioning, network connectivity listeners, hardware back button.


1. Device & Location API Replacements

Cordova Device & GPS
// Device model
console.log(device.model, device.platform, device.version);

// Geolocation
navigator.geolocation.getCurrentPosition(
  pos => console.log(pos.coords.latitude),
  err => console.error(err)
);

// Hardware back button
document.addEventListener('backbutton', onBack, false);
React Native & Expo
import * as Device from 'expo-device';
import * as Location from 'expo-location';
import { BackHandler } from 'react-native';

// Device Info
console.log(Device.modelName, Device.osName, Device.osVersion);

// Geolocation
const { status } = await Location.requestForegroundPermissionsAsync();
if (status === 'granted') {
  const loc = await Location.getCurrentPositionAsync({});
  console.log(loc.coords.latitude, loc.coords.longitude);
}

// Hardware back button
BackHandler.addEventListener('hardwareBackPress', () => {
  // Return true to prevent default back behavior
  return true;
});