Skip to content

Instantly share code, notes, and snippets.

@victory-sokolov
Created November 5, 2020 09:14
Show Gist options
  • Save victory-sokolov/7dca6d12039689ecfabda2235a13ce83 to your computer and use it in GitHub Desktop.
Save victory-sokolov/7dca6d12039689ecfabda2235a13ce83 to your computer and use it in GitHub Desktop.
React Native WebView frontal camera access
import React from 'react';
import {
StyleSheet,
StatusBar,
PermissionsAndroid,
} from 'react-native';
import {
Header,
Colors,
} from 'react-native/Libraries/NewAppScreen';
import WebView from 'react-native-webview';
const requestCameraPermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
{
title: "Cool Photo App Camera Permission",
message:
"Cool Photo App needs access to your camera " +
"so you can take awesome pictures.",
buttonNeutral: "Ask Me Later",
buttonNegative: "Cancel",
buttonPositive: "OK"
}
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("You can use the camera");
} else {
console.log("Camera permission denied");
}
} catch (err) {
console.warn(err);
}
};
const App: () => React$Node = () => {
const granted = PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.CAMERA,
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,
]);
return (
<>
<StatusBar barStyle="dark-content" />
<Header />
<WebView
useWebKit
javaScriptEnabled={true}
domStorageEnabled={true}
originWhitelist={['*']}
// allowsInlineMediaPlayback={true}
mediaPlaybackRequiresUserAction={false}
startInLoadingState={true}
// allowUniversalAccessFromFileURLs={true}
source={{uri: 'https://marcusbelcher.github.io/wasm-asm-camera-webgl-test/index.html'}}
onLoad={requestCameraPermission}
style={{ marginTop: 20 }}
/>
</>
);
};
const styles = StyleSheet.create({
scrollView: {
backgroundColor: Colors.lighter,
},
engine: {
position: 'absolute',
right: 0,
},
body: {
backgroundColor: Colors.white,
},
});
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment