Last active
October 19, 2018 14:57
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | |
} | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.