将IOS地址簿链接到应用程序

时间:2014-03-24 17:49:15

标签: ios iphone objective-c addressbook ios7.1

我想在我的应用中链接地址簿视图控制器,我已经在互联网上搜索并在苹果开发者指南中找到了但是他们展示了如何在点击按钮时打开联系人选择器而我不想要我希望联系人选择器成为我的故事板中的视图控制器。我想我可以通过制作自定义tableviewcontroller并从地址簿获取信息来实现这一点,还是可以直接进行?

1 个答案:

答案 0 :(得分:2)

这可以帮助您入门:

另外,不要忘记导入并链接到以下框架:

#import <AddressBookUI/AddressBookUI.h>
#import <AddressBook/AddressBook.h>

只需致电:[self requestPermissionForContacts];

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

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
    {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error)
        {
            if (granted)
            {
                dispatch_async(dispatch_get_main_queue(), ^
                {
                    [self findContacts];
                });
            }
            else
            {
                dispatch_async(dispatch_get_main_queue(), ^
                {
                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Find Contacts" message:@"To allow us to find your contacts, you will need to go to the Settings app > Privacy > Contacts, and set your app name here to On." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
                });
            }
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
    {
        [self findContacts];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Find Contacts" message:@"To allow us to find your contacts, you will need to go to the Settings app > Privacy > Contacts, and set your app name here to On." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}

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

    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBookRef);

    CFIndex numberOfPeople = CFArrayGetCount(people);

    if (numberOfPeople > 0)
    {
        NSMutableArray *mutableEmailsArray = [[NSMutableArray alloc]init];
        NSMutableArray *mutablePhonesArray = [[NSMutableArray alloc]init];

        for (int i = 0; i < numberOfPeople; i++)
        {
            ABRecordRef ref = CFArrayGetValueAtIndex(people, i);

            ABMultiValueRef emails = (__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonEmailProperty));

            ABMultiValueRef phones = (__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));

            if (ABMultiValueGetCount(emails) > 0)
            {
                [mutableEmailsArray addObjectsFromArray:((__bridge NSArray *)(ABMultiValueCopyArrayOfAllValues(emails)))];
            }

            if (ABMultiValueGetCount(phones) > 0)
            {
                [mutablePhonesArray addObjectsFromArray:((__bridge NSArray *)(ABMultiValueCopyArrayOfAllValues(phones)))];
            }
        }

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"%ld Emails %ld Phone Numbers", (unsigned long)mutableEmailsArray.count, (unsigned long)mutablePhonesArray.count] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"No Contacts" message:@"No contacts were found on your device." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}