CNContactStore.CNContactStore.authorizationStatusForEntityType不是.Authorized

时间:2016-10-13 06:24:44

标签: ios swift

我想获得 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)没有打印。但为什么。获得授权许可将是什么样的完美代码?

如果我在没有checkPermission()方法的情况下从viewDidLoad调用findContacts()方法,那么该应用程序已挂起以下代码

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

2 个答案:

答案 0 :(得分:0)

请尝试将此添加到.plist

***> val sqlContext = new org.apache.spark.sql.SQLContext(sc) val parqfile

> =sqlContext.read.parquet("location.parquet")
> 
> parqfile.registerTempTable("temptable")
> 
> val p1 =sqlContext.sql("select count(*) from temptable") 
> val p2=p1.show(100, false)

答案 1 :(得分:0)

试试这个

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 {

                }
            }
        }

    }

有关更多信息,请查看这些网址 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