如何在 ios 10.0 中授予访问电话通讯录的权限?

Posted

技术标签:

【中文标题】如何在 ios 10.0 中授予访问电话通讯录的权限?【英文标题】:How to give permission to access phone Addressbook in ios 10.0? 【发布时间】:2016-09-21 07:53:53 【问题描述】:

此应用已崩溃,因为它试图在没有使用说明的情况下访问隐私敏感数据。应用的 Info.plist 必须包含一个 NSContactsUsageDescription 键和一个字符串值,向用户解释应用如何使用这些数据。

我添加到.plist NSContactsUsageDescription

不起作用 - 设备版本:10.0

KTSContactsManager *addressBookManager = [KTSContactsManager sharedManager];
addressBookManager.delegate = self;
addressBookManager.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES]];

if ([isAddressBookTaken isEqualToString:@"false"]) 
    [addressBookManager importContacts:^(NSArray *contacts) 
        [EBLogger logWithTag:@"TBLContactManager" withBody:@"importContacts"];

        DLPhoneBook *phoneBook = [DLPhoneBook new];
        NSMutableArray *mutableArray = [NSMutableArray new];

        for (int i = 0; i < contacts.count; ++i) 
            NSDictionary *record = [contacts objectAtIndex:i];
            NSError *err = nil;
            DLContactRecord *contactRecord = [[DLContactRecord alloc] initWithDictionary:record error:&err];

            NSLog(@" %@ %@ %@ '",contactRecord.firstName, contactRecord.lastName, contactRecord.id);
         
    

【问题讨论】:

【参考方案1】:

您必须在您的 plist 中添加信息列表,

这里是各种隐私的信息列表。

在您的 plist 中添加您想要的隐私并再次检查。

并将此代码添加到您的文件中,

- (void)viewDidLoad 
    [super viewDidLoad];
    contactList=[[NSMutableArray alloc] init];
    ABAddressBookRef m_addressbook = ABAddressBookCreate();

    if (!m_addressbook) 
        NSLog(@"opening address book");
    

    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
    CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);

    for (int i=0;i &lt; nPeople;i++)  
        NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];

        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);

        //For username and surname
        ABMultiValueRef phones =(NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty);
        CFStringRef firstName, lastName;
        firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
        lastName  = ABRecordCopyValue(ref, kABPersonLastNameProperty);
        [dOfPerson setObject:[NSString stringWithFormat:@"%@ %@", firstName, lastName] forKey:@"name"];

        //For Email ids
        ABMutableMultiValueRef eMail  = ABRecordCopyValue(ref, kABPersonEmailProperty);
        if(ABMultiValueGetCount(eMail) &gt; 0) 
            [dOfPerson setObject:(NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:@"email"];

        

        //For Phone number
        NSString* mobileLabel;
        for(CFIndex i = 0; i &lt; ABMultiValueGetCount(phones); i++) 
            mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
            if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
            
                [dOfPerson setObject:(NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];
            
            else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
            
                [dOfPerson setObject:(NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];
                break ;
            

        [contactList addObject:dOfPerson];
        CFRelease(ref);
        CFRelease(firstName);
        CFRelease(lastName);
    
    NSLog(@"array is %@",contactList);
    

欲了解更多信息,请访问,

http://sugartin.info/2011/09/07/get-information-from-iphone-address-book-in-contacts/

希望对你有所帮助。

【讨论】:

嗨@KAR 感谢您的回答,我按照您的解释做了,但我一直收到同样的错误。我还删除了应用程序和驱动数据文件。 如果对您有用,请查看此链接,forums.developer.apple.com/thread/62229 + 设置 > MyApp > “联系人”没有出现!

以上是关于如何在 ios 10.0 中授予访问电话通讯录的权限?的主要内容,如果未能解决你的问题,请参考以下文章

Android 和 IOS - 如何敲掉国家代码和电话号码前的 0

高通Android 12/13 默认应用程序授予权限

iOS开发系列--通讯录蓝牙内购GameCenteriCloudPassbook系统服务开发汇总

iOS开发系列--通讯录蓝牙内购GameCenteriCloudPassbook系统服务开发汇总

如何使用 xmpp 框架和 openfire 服务器将电话簿联系人添加到 ios 中的聊天应用程序?

iOS获取通讯录 电话号码与姓名