Daniel Vicory 4093682e08
fix(Android): Don't show camera options for a file upload when they can not be used (#1210)
* Don't show camera options for a file upload that would result in nothing happening for the user.

On Android, if the application declares the camera permission, then even intents
that use the camera require permission to be granted. This is a problem for apps
that combine an in-app camera with a WebView that has file uploading and the user
has not given permission for the camera.

Note, this will not request permission for camera. This will simply prevent
showing the camera options that would be a no-op action for users. It does this
by checking if the camera permission is declared, and if so, checks that the
user has granted permission.

More information: https://blog.egorand.me/taking-photos-not-so-simply-how-i-got-bitten-by-action_image_capture/

* Add example and documentation about camera option availability in file uploads for Android.
2020-02-18 18:40:30 -08:00

70 lines
1.9 KiB
TypeScript

import React, {Component} from 'react';
import {Button, Linking, Text, View} from 'react-native';
import WebView from 'react-native-webview';
const HTML = `
<!DOCTYPE html>\n
<html>
<head>
<title>Uploads</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=320, user-scalable=no">
<style type="text/css">
body {
margin: 0;
padding: 0;
font: 62.5% arial, sans-serif;
background: #ccc;
}
</style>
</head>
<body>
<p>
<label for="images-only">Images only file upload</label>
<input name="images-only" type="file" accept="image/*">
</p>
<p>
<label for="video-only">Video only file upload</label>
<input name="video-only" type="file" accept="video/*">
</p>
<p>
<label for="any-file">Any file upload</label>
<input name="any-file" type="file">
</p>
</body>
</html>
`;
type Props = {};
type State = {};
export default class Uploads extends Component<Props, State> {
state = {};
render() {
return (
<View>
<View style={{ height: 120 }}>
<WebView
source={{html: HTML}}
automaticallyAdjustContentInsets={false}
/>
</View>
<Text>
Android limitation: If the file input should show camera options for the user,
and the app has the ability to request the camera permission, then the user must
grant permission first in order to see the options. Since this example app does
have the permission declared, you must allow it in settings to be able to see
camera options. If your app does not have the camera permission declared, then
there is no restriction to showing the camera options.
</Text>
<Button
title="Open settings"
onPress={() => Linking.openSettings()}
/>
</View>
);
}
}