首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CNContactStore.CNContactStore.authorizationStatusForEntityType不是.Authorized

CNContactStore.CNContactStore.authorizationStatusForEntityType不是.Authorized
EN

Stack Overflow用户
提问于 2016-10-13 06:24:44
回答 2查看 771关注 0票数 0

我在试着让CNContact。但问题是,CNContactStore.CNContactStore.authorizationStatusForEntityType在任何时候都不是.Authorized

代码语言:javascript
复制
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()不称为ever.That意思是打印(cnt.count),不打印。但是为什么呢。对于获得授权许可,什么才是最完美的代码?

如果我从没有findContacts()方法的viewDidLoad调用checkPermission()方法,那么应用程序挂起了下面的代码

代码语言:javascript
复制
try store.enumerateContactsWithFetchRequest(fetchRequest, usingBlock: { (let contact, let stop) -> Void in
                    contacts.append(contact)
                })
EN

回答 2

Stack Overflow用户

发布于 2016-10-13 07:10:56

请把这个添加到.plist中

代码语言:javascript
复制
<key>NSContactsUsageDescription</key>
    <string>This app requires contacts from phone book.</string>
票数 0
EN

Stack Overflow用户

发布于 2016-10-22 10:16:25

尝尝这个

代码语言:javascript
复制
func initContacts() {
//        titleArr.removeAll();
        CoreContextNDelegate.sharedInstance().requestForAccess { (accessGranted) -> Void in
            if accessGranted {
                //let predicate = CNContact.predicateForContactsMatchingName("Joshna")
                let keys = [
                    CNContactFormatter.descriptorForRequiredKeys(for: .fullName),
                    CNContactGivenNameKey, CNContactFamilyNameKey,
                    CNContactEmailAddressesKey,
                    CNContactPhoneNumbersKey,
                    CNContactImageDataAvailableKey,
                    CNContactThumbnailImageDataKey
                ] as [Any]
//                let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey, CNContactBirthdayKey, CNContactImageDataKey]
                let request = CNContactFetchRequest(keysToFetch: keys as! [CNKeyDescriptor]);
                //var contacts = [CNContact]()
                var message: String!

                //let contactsStore = CoreContextNDelegate.sharedInstance().getContactStore();
                do {
                    //   contacts = try contactsStore.unifiedContactsMatchingPredicate(predicate, keysToFetch: keys)
                    try CoreContextNDelegate.sharedInstance().getContactStore().enumerateContacts(with: request) {
                        contact, stop in
                        print(contact.givenName)
                        print(contact.familyName)
                        print(contact.identifier)

                        for phoneNumber in  contact.phoneNumbers {
                            let contacts: Contact = Contact()!
                            contacts.givenName = contact.givenName+" "+contact.middleName+" "+contact.familyName
                            if let value = phoneNumber.value as? CNPhoneNumber {
                                if let digit = value.value(forKey: "digits") as? String {
                                    contacts.phoneNumbers = digit;

                                }
                            }
                            self.contactList.append(contacts)
                        }

                        self.filteredContactList = self.contactList;
                    }
                    DispatchQueue.main.async {
                        self.initTableViewData();
                    }
                }
                catch {
                    message = "Unable to fetch contacts."
                    DispatchQueue.main.async {
                        self.initTableViewData();
                    }

                }


                if message != nil {
                    DispatchQueue.main.async(execute: { () -> Void in
                        print(message);
                        //AppDelegate.getAppDelegate().showMessage(message)
                    })
                }
                else {

                }
            }
        }

    }

有关更多信息,请查看这些urls https://developer.apple.com/reference/contacts

https://www.safaribooksonline.com/library/view/ios-9-swift/9781491936689/ch04.html

https://code.tutsplus.com/tutorials/ios-9-an-introduction-to-the-contacts-framework--cms-25599

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40013708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档