【问题标题】:Issue about the newest Contacts ios Framework关于最新联系人 ios 框架的问题
【发布时间】:2015-10-26 13:22:32
【问题描述】:

我正在开发我的应用程序,并且在某个时候用户可以说服他们的朋友下载它。但是,ABAddressBook 框架 (link) 在 iOS 9 中已被弃用,所以我不得不自学最新的 Contacts 框架 (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 询问用户的方法吗?

【问题讨论】:

    标签: ios objective-c contacts cncontact


    【解决方案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
            }
    

    【讨论】:

    • 感谢您的出色回答,但正是在我的问题中:如何请求/询问该授权?怎么办?这和用户的位置是一样的吗?
    猜你喜欢
    • 1970-01-01
    • 2016-06-06
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    相关资源
    最近更新 更多