坚持在IOS中从PhoneBook访问联系人

时间:2015-02-25 06:55:02

标签: ios objective-c contacts abaddressbook

我想在IOS中访问电话簿中的所有联系人。我试过下面的代码

    CFErrorRef error = NULL;

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

    if (addressBook != nil)
    {
        NSLog(@"Succesful.");

        NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

    }

此代码运行良好,所有联系人都存储在" allContacts"在我的演示项目中的数组但是当我把这个代码放在我现有的项目中时,它返回" nil"记录。 下面是我现有项目的标题和实现文件,实际上我想要使用它。

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

@interface TestingViewController : UIViewController
-(IBAction)GetContacts;
-(void)GetPBAccess;
@end


    //.m file
    #import "TestingViewController.h"
    #import <AddressBook/AddressBook.h>
    #import <AddressBookUI/AddressBookUI.h>

    @interface TestingViewController ()

    @end

    @implementation TestingViewController

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        [self GetPBAccess];
    }
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    -(void)GetPBAccess
{
    ABAddressBookRef addressBook1 = ABAddressBookCreateWithOptions(NULL, NULL);
    switch (ABAddressBookGetAuthorizationStatus()) {
        case kABAuthorizationStatusNotDetermined:
        {
            ABAddressBookRequestAccessWithCompletion(addressBook1, ^(bool granted, CFErrorRef error) {
                if (granted) {
                    NSLog(@"Access Granted");
                    [self GetContacts];
                }
                else{
                    NSLog(@"Access Not Granted");
                }
            });
            break;
        }
        case kABAuthorizationStatusAuthorized:
        {
            NSLog(@"AUTHORIZATION ALREADY Granted");
            [self GetContacts];
            break;
        }
        case kABAuthorizationStatusDenied:
        {
            NSLog(@"AUTHORIZATION DENIED");
            break;
        }

        default:
            break;
    }
}


    -(IBAction)GetContacts
    {
        CFErrorRef error = NULL;

        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

        if (addressBook != nil)
        {
            NSLog(@"Succesful.");

            NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

            NSUInteger i = 0;
            for (i = 0; i < [allContacts count]; i++)
            {
                //Person *person = [[Person alloc] init];

                ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];

                NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
                NSString *lastName =  (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
                NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];

                //            person.firstName = firstName;
                //            person.lastName = lastName;
                //            person.fullName = fullName;

                //email
                ABMultiValueRef emails = ABRecordCopyValue(contactPerson, kABPersonEmailProperty);

                //            NSUInteger j = 0;
                //            for (j = 0; j < ABMultiValueGetCount(emails); j++)
                //            {
                //                NSString *email = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(emails, j);
                //                if (j == 0)
                //                {
                //                    person.homeEmail = email;
                //                    NSLog(@"person.homeEmail = %@ ", person.homeEmail);
                //                }
                //
                //                else if (j==1)
                //                    person.workEmail = email;
                //            }
                //
                //            [self.tableData addObject:person];
            }
        }

        CFRelease(addressBook);
    }


    /*
    #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    @end

1 个答案:

答案 0 :(得分:0)

问题看起来是因为你打电话

    [self GetPBAccess];
    [self GetContacts];

直接内联。您应该做的是将GetContacts调用移动到GetPBAccess,以便在您知道自己有权访问后调用它(如果您没有访问权限则不会被调用)。

目前它将在用户授予访问权限之前运行,当然是第一次尝试访问,并且什么都不返回。