gpt4 book ai didi

ios - CNContactStore.CNContactStore.authorizationStatusForEntityType 不是 .Authorized

转载 作者:行者123 更新时间:2023-11-30 12:59:29 25 4
gpt4 key购买 nike

我正在尝试获取CNContact。但问题是 CNContactStore.CNContactStore.authorizationStatusForEntityType 任何时候都不是.Authorized

import Contacts
import ContactsUI

class ContactViewController: UIViewController
var store = CNContactStore()
override func viewDidLoad() {
checkPermission()
}



func checkPermission(){
switch CNContactStore.authorizationStatusForEntityType(.Contacts){
case .Authorized:
let cnt = findContacts()
print(cnt.count)

case .NotDetermined:
store.requestAccessForEntityType(.Contacts){succeeded, err in
guard err == nil && succeeded else{
return
}
let cnt = findContacts()
print(cnt.count)
}
default:
print("Not Handled")
}
}

func findContacts() -> [CNContact] {

let keysToFetch = [CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName),
CNContactImageDataKey,CNContactEmailAddressesKey,CNContactUrlAddressesKey,CNContactNoteKey,
CNContactPhoneNumbersKey,CNContactPostalAddressesKey]

let fetchRequest = CNContactFetchRequest(keysToFetch: keysToFetch)
fetchRequest.unifyResults = true
var contacts = [CNContact]()
do {
try store.enumerateContactsWithFetchRequest(fetchRequest, usingBlock: { (let contact, let stop) -> Void in
contacts.append(contact)
})
}
catch let error as NSError {
print(error.localizedDescription)
}

return contacts
}
}

checkPermission() 方法 findContacts() 中从未被调用过。这意味着 print(cnt.count) 不会打印。但为什么。为了获得授权许可,完美的代码是什么?

如果我从 viewDidLoad 调用 findContacts() 方法而不使用 checkPermission() 方法,则应用程序已卡在以下代码上

try store.enumerateContactsWithFetchRequest(fetchRequest, usingBlock: { (let contact, let stop) -> Void in
contacts.append(contact)
})

最佳答案

请尝试将其添加到 .plist

<key>NSContactsUsageDescription</key>
<string>This app requires contacts from phone book.</string>

关于ios - CNContactStore.CNContactStore.authorizationStatusForEntityType 不是 .Authorized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40013708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com