How to give permission to access phone Addressbook in ios 10.0?

∥☆過路亽.° 提交于 2019-12-02 20:15:48

问题


This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSContactsUsageDescription key with a string value explaining to the user how the app uses this data.

I added to the .plist NSContactsUsageDescription

doesn't work - device version: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:


You have to add information list in your plist,

Here is information plist for various privacy.

Add Privacy that you want in you plist and check again.

and ADD this code in your file,

- (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);
    }
}

For more about it visit,

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

Hope it will help you.



来源:https://stackoverflow.com/questions/39610590/how-to-give-permission-to-access-phone-addressbook-in-ios-10-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!