关于最新的 Contacts ios Framework 的问题

Issue about the newest Contacts ios Framework

我正在开发我的应用程序,在某个时候用户可以说服他们的朋友下载它。但是,ABAddressBook 框架 (link) has been deprecated with iOS 9, so I had to teach myself the newest Contacts framework (link).

但是,我仍然面临问题。到目前为止,我已经阅读了文档:

NSArray *keysToFetch = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];

    NSString *containerId = [self.CN_contacts defaultContainerIdentifier];

    NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];

    self.allContacts = [self.CN_contacts unifiedContactsMatchingPredicate:predicate keysToFetch:keysToFetch error:nil];

但我知道代码块缺少要求用户授予对其联系人的访问权限的功能。

有谁知道用 CNAuthorizationStatus 询问用户的方法吗?

你必须这样使用它

switch CNContactStore.authorizationStatusForEntityType(CNEntityType.Contacts)
        {
            case CNAuthorizationStatus.Denied,CNAuthorizationStatus.Restricted :
             //Handle denied and restricted case
             break
            case CNAuthorizationStatus.NotDetermined :
             //Request Access
            contactsStore?.requestAccessForEntityType(CNEntityType.Contacts, completionHandler: { (granted, error) -> Void in
                //At this point an alert is provided to the user to provide access to contacts. 
                //This will get invoked if a user responds to the alert
                if  (!granted ){
                    //User has allowed the access in the alertview provided
                }
                else{
                    //User has decline the access in the alertview provided
                }
            })
             break
            case  CNAuthorizationStatus.Authorized :
             //Do your stuff

             NSArray *keysToFetch = @[CNContactGivenNameKey,CNContactFamilyNameKey, CNContactPhoneNumbersKey];
             NSString *containerId = [self.CN_contacts defaultContainerIdentifier];
             NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
             self.allContacts = [self.CN_contacts unifiedContactsMatchingPredicate:predicate keysToFetch:keysToFetch error:nil];

             break
        }