Created
September 27, 2016 20:38
-
-
Save roblav96/52e453c942c029f52bfad824b9012adc to your computer and use it in GitHub Desktop.
Get contacts permissions on ios10
This file contains hidden or 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 class PermissionsClass { | |
| private static _contactStore: CNContactStore = CNContactStore.alloc().init() | |
| private _getContactsAuthorization(): CNAuthorizationStatus { | |
| return CNContactStore.authorizationStatusForEntityType(CNEntityType.Contacts) | |
| } | |
| isContactsAuthorized(): boolean { | |
| return this._getContactsAuthorization() == CNAuthorizationStatus.Authorized | |
| } | |
| isContactsDenied(): boolean { | |
| let status: CNAuthorizationStatus = this._getContactsAuthorization() | |
| let denied: boolean = ( | |
| status == CNAuthorizationStatus.Denied | |
| || | |
| status == CNAuthorizationStatus.Restricted | |
| ) | |
| return denied | |
| } | |
| requestContactsAuthorization(): Promise<boolean> { | |
| if (this._getContactsAuthorization() == CNAuthorizationStatus.Authorized) { | |
| return Promise.resolve(true) | |
| } else { | |
| return new Promise((resolve) => { | |
| PermissionsClass._contactStore.requestAccessForEntityTypeCompletionHandler(CNEntityType.Contacts, function completionHandler(result: boolean, error: NSError): void { | |
| if (error) { | |
| global.tnsconsole.error('requestAccessForEntityTypeCompletionHandler > error', error) | |
| resolve(false) | |
| } else { | |
| resolve(result) | |
| } | |
| }) | |
| }) | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Then add to your
info.plist: