MM-9926 Fix iOS crash caused by permissions (#1535)

This commit is contained in:
Elias Nahum 2018-03-22 19:08:59 +02:00 committed by Saturnino Abril
parent 3bd7275cfa
commit f56bdb3090

View file

@ -2,13 +2,16 @@ import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import {injectIntl, intlShape} from 'react-intl';
import {
Alert,
Platform,
StyleSheet,
TouchableOpacity,
} from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';
import ImagePicker from 'react-native-image-picker';
import Permissions from 'react-native-permissions';
import {PermissionTypes} from 'app/constants';
import {changeOpacity} from 'app/utils/theme';
class AttachmentButton extends PureComponent {
@ -29,7 +32,7 @@ class AttachmentButton extends PureComponent {
maxFileCount: 5,
};
attachFileFromCamera = () => {
attachFileFromCamera = async () => {
const {formatMessage} = this.props.intl;
const options = {
quality: 1.0,
@ -55,13 +58,17 @@ class AttachmentButton extends PureComponent {
},
};
ImagePicker.launchCamera(options, (response) => {
if (response.error || response.didCancel) {
return;
}
const hasPhotoPermission = await this.hasPhotoPermission();
this.uploadFiles([response]);
});
if (hasPhotoPermission) {
ImagePicker.launchCamera(options, (response) => {
if (response.error || response.didCancel) {
return;
}
this.uploadFiles([response]);
});
}
};
attachFileFromLibrary = () => {
@ -131,6 +138,59 @@ class AttachmentButton extends PureComponent {
});
};
hasPhotoPermission = async () => {
if (Platform.OS === 'ios') {
const {formatMessage} = this.props.intl;
let permissionRequest;
const hasPermissionToStorage = await Permissions.check('photo');
switch (hasPermissionToStorage) {
case PermissionTypes.UNDETERMINED:
permissionRequest = await Permissions.request('photo');
if (permissionRequest !== PermissionTypes.AUTHORIZED) {
return false;
}
break;
case PermissionTypes.DENIED: {
const canOpenSettings = await Permissions.canOpenSettings();
let grantOption = null;
if (canOpenSettings) {
grantOption = {
text: formatMessage({
id: 'mobile.android.permission_denied_retry',
defaultMessage: 'Set permission',
}),
onPress: () => Permissions.openSettings(),
};
}
Alert.alert(
formatMessage({
id: 'mobile.android.photos_permission_denied_title',
defaultMessage: 'Photo library access is required',
}),
formatMessage({
id: 'mobile.android.photos_permission_denied_description',
defaultMessage: 'To upload images from your library, please change your permission settings.',
}),
[
grantOption,
{
text: formatMessage({
id: 'mobile.android.permission_denied_dismiss',
defaultMessage: 'Dismiss',
}),
},
]
);
return false;
}
}
}
return true;
};
uploadFiles = (images) => {
this.props.uploadFiles(images);
};