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

1

8

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

Airlike answered 7/7, 2016 at 8:55 Comment(0)
S
25

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)
}
Swanner answered 7/7, 2016 at 11:49 Comment(1)
such a simple solution. I cant believe i missed that! thanks a lotAirlike

© 2022 - 2024 — McMap. All rights reserved.