首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在IOS中无法从PhoneBook访问联系人

在IOS中无法从PhoneBook访问联系人
EN

Stack Overflow用户
提问于 2015-02-25 14:55:02
回答 1查看 336关注 0票数 0

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

代码语言:javascript
复制
    CFErrorRef error = NULL;

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

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

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

    }

这段代码运行良好,所有联系人都存储在我的演示项目中的"allContacts“数组中,但是当我将这段代码放入现有项目中时,它返回了"nil”记录。下面是我现有项目的头文件和实现文件,实际上我想在其中使用它。

代码语言:javascript
复制
//.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
EN

回答 1

Stack Overflow用户

发布于 2015-02-25 15:07:38

问题看起来是因为您调用了

代码语言:javascript
复制
    [self GetPBAccess];
    [self GetContacts];

直接内联。您应该做的是将GetContacts调用移到GetPBAccess中,这样一旦您知道您拥有访问权限,就会调用它(如果您没有访问权限,就不会调用它)。

目前,它将在用户授予访问权限之前运行,当然是在您第一次尝试它时,并且不返回任何信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28712971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档