0

我正在开发我的应用程序,并且在某个时候,用户可以说服他们的朋友下载它。但是,该ABAddressBook框架(链接)已在 iOS 9 中弃用,所以我不得不自学最新的Contacts框架(链接)

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

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

4

1 回答 1

1

你必须像这样使用它

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
        }
于 2015-10-26T13:31:41.230 回答