Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 / Xcode 8 - CNContact [access] <Private>

My code crashes as soon as it tries to request access to the CNContactStore. Any ideas if this is a beta issue?

var addressBookStore = CNContactStore()

addressBookStore.requestAccess(for: .contacts) { (granted, error)

in

// This console message is triggered at the crash - Messenger[836:1175155] [access] private

the crash occurs at this line and even preventing even printing the error!

Thanks in advance

like image 674
knappsimon Avatar asked Jul 07 '16 08:07

knappsimon


1 Answers

As suggested here : https://developer.apple.com/reference/contacts

Important

An iOS app linked on or after iOS 10.0 must include in its Info.plist file the usage description keys for the types of data it needs to access or it will crash. To access Contacts data specifically, it must include NSContactsUsageDescription.

You have to addd NSContactsUsageDescription key in your Info.plist file

enter image description here

Then you will get authorization dialog. Without this key app will crash.

enter image description here

let addressBookStore = CNContactStore()

addressBookStore.requestAccess(for: CNEntityType.contacts) { (isGranted, error) in
    print(isGranted)
    print(error)
}
like image 117
Ashish Kakkad Avatar answered Oct 19 '22 14:10

Ashish Kakkad