Skip to content

Instantly share code, notes, and snippets.

@alexsullivan114
Last active October 19, 2018 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexsullivan114/21e5a8922dbf2a358bab0842f17ea19a to your computer and use it in GitHub Desktop.
Save alexsullivan114/21e5a8922dbf2a358bab0842f17ea19a to your computer and use it in GitHub Desktop.
Moving from the old Android permissions model to the new permissions model in React Native.
export default class ContactsDisplay extends Component {
render() {
return (
<View>
<Button title="Press me for contacts!" onPress={this.getContacts} />
</View>
)
}
async requestContactsPermission() {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
{
title: 'Hey you need to give us contacts permissions!',
message: 'We need to read your contacts so we can sell them to advertisers.'
}
)
return granted === PermissionsAndroid.RESULTS.GRANTED
}
getContacts = () => {
this.requestContactsPermission()
.then(function (didGetPermission: boolean) {
if (didGetPermission) {
Contacts.getAll((err, contacts) => {
if (err) throw err;
alert("We got some contacts!")
})
} else {
alert("Oh no no permissions!")
}
})
}
}
export default class ContactsDisplay extends Component {
render() {
return (
<View>
<Button title="Press me for contacts!" onPress={this.getContacts} />
</View>
)
}
getContacts = () => {
Contacts.getAll((err, contacts) => {
if (err) throw err;
alert("We got some contacts!")
})
}
}
@alexsullivan114
Copy link
Author

This is a small example of moving from a world (on Android) where all permissions are granted at app install time to one where you have to actively request them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment