代码之家  ›  专栏  ›  技术社区  ›  Christian Mandl

如何获取联系人ID

  •  2
  • Christian Mandl  · 技术社区  · 9 年前

    我无法从通讯簿中获取联系人ID。

    我的.m文件:

    -(IBAction)test:(id)sender
    {
        ABPeoplePickerNavigationController* picker = [[ABPeoplePickerNavigationController alloc] init];
        picker.peoplePickerDelegate = self;
        [self presentViewController:picker animated:YES completion:nil];
    }
    
    -(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
    {
        [peoplePicker dismissViewControllerAnimated:YES completion:nil];
        ABRecordID recordID = ABRecordGetRecordID(person);
        label.text = [NSString stringWithFormat:@"%d", recordID];
    
    }
    
    -(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
    {
        [peoplePicker dismissViewControllerAnimated:YES completion:nil];
    }
    

    我将AddressBook和AddressBookUI导入到.h文件中。 这一切都有效,但我得到了错误的ID。 每次我只得到ID-1,这代表错误。

    希望你能帮助我。

    谢谢, 基督教的

    2 回复  |  直到 9 年前
        1
  •  1
  •   Rob Md Fahim Faez Abir    9 年前

    在iOS 8中,当只显示人员选择器时,它不再自动请求地址簿的权限。(其想法是,仅选择者并不能真正让应用程序访问您的联系人,因此不需要任何权限。当应用程序无法真正获取联系人详细信息时,它提供了低摩擦的UI。)

    但是,在您的案例中,您确实在尝试以编程方式检索有关联系人的信息。为此,您确实需要许可。因此,在呈现选择器之前,您可能需要请求地址簿的权限:

    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
    if ((status == kABAuthorizationStatusDenied || status == kABAuthorizationStatusRestricted)) {
        NSLog(@"User previously denied permission; show error message that they must go to settings and give this app permission");
        return;
    } else if (status == kABAuthorizationStatusNotDetermined) {
        CFErrorRef error;
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
        if (!addressBook) {
            NSLog(@"unable to open address book: %@", CFBridgingRelease(error));
            return;
        }
    
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            if (granted) {
                NSLog(@"OK");
            } else {
                NSLog(@"Not ok");
            }
    
            CFRelease(addressBook);
        });
    }
    
        2
  •  0
  •   Christian Mandl    9 年前

    我的解决方案是如何获取联系人的ID:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
        if ((status == kABAuthorizationStatusDenied || status == kABAuthorizationStatusRestricted)) {
            NSLog(@"User previously denied permission; show error message that they must go to settings and give this app permission");
            return;
        } else if (status == kABAuthorizationStatusNotDetermined) {
            CFErrorRef error;
            ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
            if (!addressBook) {
                NSLog(@"unable to open address book: %@", CFBridgingRelease(error));
                return;
            }
    
            ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
                if (granted) {
                    NSLog(@"OK");
                } else {
                    NSLog(@"Not ok");
                }
    
                CFRelease(addressBook);
            });
        }
    
        ABPeoplePickerNavigationController* picker = [[ABPeoplePickerNavigationController alloc] init];
        picker.peoplePickerDelegate = self;
        [self presentViewController:picker animated:YES completion:nil];
    }
    
    -(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
    {
        [peoplePicker dismissViewControllerAnimated:YES completion:nil];
        ABRecordID recordID = ABRecordGetRecordID(person);
        NSLog([NSString stringWithFormat:@"%@"], recordID);
    }
    

    感谢帮助我的Rob:)