如何在Xcode中检测允许联系按钮操作

时间:2015-09-03 08:32:13

标签: ios xcode

在我的应用程序中,我检索手机联系人列表并在UITableView中显示其名称。作为检索联系人列表,我们都知道,首次显示“允许联系人”弹出窗口。如果按“允许”按钮,则不会立即显示联系人。我需要回去再来这个屏幕。无论如何,按“允许”按钮时会显示联系人吗?

-(void)collectContacts {
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
            if (granted) {
                ABRecordRef source = ABAddressBookCopyDefaultSource(addressBookRef);
                CFArrayRef allPeople = (ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBookRef, source, kABPersonSortByFirstName));
                CFIndex nPeople = CFArrayGetCount(allPeople);

                for (int i = 0; i < nPeople; i++) {

                    @autoreleasepool
                    {

                        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

                        //get First Name
                        CFStringRef firstName = (CFStringRef)ABRecordCopyValue(person, kABPersonFirstNameProperty);
                        CFStringRef lastName = (CFStringRef)ABRecordCopyValue(person, kABPersonLastNameProperty);
                        ABMultiValueRef multiPhones = ABRecordCopyValue(person,kABPersonPhoneProperty);
                        CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, 0);
                        NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;


                        CFDataRef imgData = ABPersonCopyImageData(person);
                        NSData *imageData = (__bridge NSData *)imgData;

                        NSString *firstname = [(__bridge NSString*)firstName copy];
                        NSString *lastname = [(__bridge NSString*)lastName copy];
                        if (!lastname || lastname == [NSNull null]) {
                            lastname = @"";
                        }

                        NSLog(@"%@ %@ %@", firstname, lastname, phoneNumber);

                        NSString *fullname = [NSString stringWithFormat:@"%@ %@", firstname, lastname];

                        ContactObject *contactObj = [[ContactObject alloc] initWithUniqueId:[UIImage imageWithData:imageData] title:fullname phonenumber:phoneNumber];
                        [_objects addObject:contactObj];
                    }
                }
            }
        });

    } else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        ABRecordRef source = ABAddressBookCopyDefaultSource(addressBookRef);
        CFArrayRef allPeople = (ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBookRef, source, kABPersonSortByFirstName));
        CFIndex nPeople = CFArrayGetCount(allPeople);

        for (int i = 0; i < nPeople; i++) {

            @autoreleasepool
            {

                ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

                //get First Name
                CFStringRef firstName = (CFStringRef)ABRecordCopyValue(person,kABPersonFirstNameProperty);
                CFStringRef lastName = (CFStringRef)ABRecordCopyValue(person,kABPersonLastNameProperty);
                CFDataRef imgData = ABPersonCopyImageData(person);
                NSData *imageData = (__bridge NSData *)imgData;
                ABMultiValueRef multiPhones = ABRecordCopyValue(person,kABPersonPhoneProperty);
                CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, 0);
                NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;

                NSString *firstname = [(__bridge NSString*)firstName copy];
                NSString *lastname = [(__bridge NSString*)lastName copy];

                if (!lastname || lastname == [NSNull null]) {
                    lastname = @"";
                }

                NSString *fullname = [NSString stringWithFormat:@"%@ %@", firstname, lastname];

                ContactObject *contactObj = [[ContactObject alloc] initWithUniqueId:[UIImage imageWithData:imageData] title:fullname phonenumber:phoneNumber];
                [_objects addObject:contactObj];
              }
        }
    }

    [_contactTable reloadData];
}

2 个答案:

答案 0 :(得分:0)

处理此问题的正确方法是使用:

void ABAddressBookRequestAccessWithCompletion(ABAddressBookRef addressBook, ABAddressBookRequestAccessCompletionHandler completion);

在获得访问权限之前,您无能为力。您只需要等到调用完成处理程序。在那个时候(取决于)你是否被授予访问权限,你可以填充你的表,而不是之前。

您可以在使用之前或之后的任何时候检查您的访问权限:

ABAuthorizationStatus ABAddressBookGetAuthorizationStatus(void);

来自Apple文档:

  

讨论

     

使用此功能请求访问地址簿数据。在要求用户访问时,此调用不会阻止,从而允许您的应用继续运行。在授予访问权限之前,您的应用程序所引用的任何通讯簿引用都不会包含任何数据,并且任何修改数据的尝试都将失败,错误类型为kABOperationNotPermittedByUserError。第一次请求访问时,仅向用户请求权限。以后的呼叫使用用户授予的权限。

     

在任意队列上调用完成处理程序。如果您的应用在整个应用中使用地址簿,则您有责任确保将该地址簿的所有使用情况分发到单个队列,以确保正确的线程安全操作。

答案 1 :(得分:0)

您需要以编程方式显示权限警报,一旦允许,您需要在UITableView中获取联系人并加载:

ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (!granted){
                    [[[UIAlertView alloc] initWithTitle:@"Contacts Access Denied"
                                                message:@"This app requires access to your device's Contacts.\n\nPlease enable Contacts access for this app in Settings / Privacy / Contacts"
                                               delegate:nil
                                      cancelButtonTitle:@"Dismiss"
                                      otherButtonTitles:nil] show];
            } else {
                //access authorized, fetch and display
            }
        });
    });