diff --git a/README.md b/README.md index 4a2d2af..7f88bb3 100644 --- a/README.md +++ b/README.md @@ -240,6 +240,44 @@ await BarcodeScanner.setFlash(false); // Turn off --- +#### `hasCameraPermission()` + +Checks if camera permission is currently granted. + +```typescript +const hasPermission = await BarcodeScanner.hasCameraPermission(); +console.log('Camera permission granted:', hasPermission); +``` + +**Returns:** `Promise` - `true` if permission granted, `false` otherwise + +--- + +#### `requestCameraPermission()` + +Requests camera permission from the user with native promise resolution. + +```typescript +const granted = await BarcodeScanner.requestCameraPermission(); +if (granted) { + console.log('Permission granted!'); + // Start scanning +} else { + console.log('Permission denied'); + // Show error or guide user to settings +} +``` + +**Returns:** `Promise` - `true` if user grants permission, `false` if denied + +**Platform Support:** +- ✅ **iOS**: Fully supported with native callback +- ✅ **Android**: Fully supported with native callback (API 23+) + +**Note:** This method shows the native system permission dialog and waits for the user's response, then resolves the promise based on their choice. + +--- + ### `CameraView` React component that renders the camera preview. @@ -303,9 +341,99 @@ useEffect(() => { ### Permission Handling -> **⚠️ Important:** We **strongly recommend** using [`react-native-permissions`](https://github.com/zoontek/react-native-permissions) for handling camera permissions in production apps. This provides better UX, more control, and unified API across platforms. +The library now provides **built-in native camera permission methods** that work seamlessly on both iOS and Android with proper promise resolution based on user response. + +#### ✅ Using Built-in Permission Methods (Recommended) + +The library includes native methods that handle camera permissions with proper callbacks: + +```tsx +import React, { useEffect, useState } from 'react'; +import { View, Text, Button, Alert } from 'react-native'; +import { BarcodeScanner, CameraView } from '@pushpendersingh/react-native-scanner'; + +export default function App() { + const [hasPermission, setHasPermission] = useState(null); + const [scanning, setScanning] = useState(false); + + useEffect(() => { + checkPermission(); + }, []); + + const checkPermission = async () => { + const granted = await BarcodeScanner.hasCameraPermission(); + setHasPermission(granted); + }; + + const requestPermission = async () => { + const granted = await BarcodeScanner.requestCameraPermission(); + setHasPermission(granted); + + if (granted) { + Alert.alert('Success', 'Camera permission granted!'); + } else { + Alert.alert( + 'Permission Denied', + 'Camera permission is required to scan barcodes' + ); + } + }; + + const startScanning = async () => { + // Check permission before scanning + const granted = await BarcodeScanner.hasCameraPermission(); + + if (!granted) { + Alert.alert( + 'Permission Required', + 'Please grant camera permission to scan barcodes', + [{ text: 'Grant Permission', onPress: requestPermission }] + ); + return; + } + + setScanning(true); + await BarcodeScanner.startScanning((barcode) => { + console.log('Scanned:', barcode); + BarcodeScanner.stopScanning(); + setScanning(false); + }); + }; + + if (hasPermission === null) { + return Checking camera permission...; + } + + if (hasPermission === false) { + return ( + + Camera permission not granted +