IOS - 获取联系电话用于地址簿

时间:2013-09-10 07:41:59

标签: ios objective-c addressbook

我有一台设备手机运行ios 6.0.1,我希望在我的设备中获取所有联系人。我尝试将iphone用于IOS 5.1,6.1.3并且它的工作正常。不幸的是,当它运行ios 6.0.1时,数据为空

这是我的代码:

  

if([[[[UIDevice currentDevice] systemVersion] floatValue]> 5.2){

    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
    {
        ABAddressBookRequestAccessWithCompletion(addressBookRef,
                                                 ^(bool granted, CFErrorRef error) {
                                                     if (granted)
                                                         [self loadContact];

                                                 });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
    {
        [self loadContact];
    }
}
else
{
    [self loadContact];
}
     

- (void)loadContact {

ABAddressBookRef addressBooks = ABAddressBookCreate();
allPeople = (__bridge NSArray *)(ABAddressBookCopyArrayOfAllPeople(addressBooks));
peopleCount = ABAddressBookGetPersonCount(addressBooks); }

我不知道,为什么它不适用于ios 6.0.1

我找到this link并执行此操作,但我的设备没有数据。

你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

代码中最大的问题是您需要将引用“addressBookRef”传递给“loadContact”方法。 “ABAddressBookCreate”不适用于iOS6 - 您需要使用ABAddressBookRequestAccessWithCompletion创建的那个。

顺便说一下。而不是检查iOS版本更好地使用它来确定您是否需要请求权限。这是我正在使用的代码 - 随意使用它:

-(BOOL)isABAddressBookCreateWithOptionsAvailable {
    return &ABAddressBookCreateWithOptions != NULL;
}

- (void) importContactsFromAddressBook
{
    ABAddressBookRef addressBook;
    if ([self isABAddressBookCreateWithOptionsAvailable]) {
        CFErrorRef error = nil;
        addressBook = ABAddressBookCreateWithOptions(NULL,&error);
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            // callback can occur in background, address book must be accessed on thread it was created on
                if (error) {
                    [self.delegate addressBookHelperError:self];
                } else if (!granted) {
                    [self.delegate addressBookHelperDeniedAcess:self];
                    AddressBookUpdated(addressBook, nil, self);
                } else {
                    // access granted
                    AddressBookUpdated(addressBook, nil, self);
                    CFRelease(addressBook);
                }
        });
    } else {
        // iOS 4/5
        addressBook = ABAddressBookCreate();
        AddressBookUpdated(addressBook, NULL, self);
        CFRelease(addressBook);
    }
}

void AddressBookUpdated(ABAddressBookRef addressBook, CFDictionaryRef info, void *context)
{
    NSMutableArray* addressBookContacts = [NSMutableArray array];

    //import from address book
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
    CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );
    [helper.delegate performSelectorOnMainThread:@selector(didLoadList:)
                                    withObject:[NSNumber numberWithInt:nPeople]
                                 waitUntilDone:YES];
    if(allPeople) CFRelease(allPeople);
    [helper.delegate performSelectorOnMainThread:@selector(didComplete:)
                                    withObject:addressBookContacts
                                 waitUntilDone:YES];
}

我将这些函数封装到类中,委托是:

@protocol ContactImporterProgressViewDelegate <NSObject>
- (void) didLoadList:(NSNumber*) totalItems;
- (void) updateProgress:(NSNumber*) progress;
- (void) didComplete:(NSArray*) contactsImported;
@end

@property (nonatomic, assign) NSObject <ContactImporterProgressViewDelegate>* delegate;

要使用它我建议在另一个线程上调用它来阻止UI并显示进度(当你有5000个条目时需要一段时间):

ContactImporter* importer = [[ContactImporter alloc] init];
importer.delegate = self;
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
NSInvocationOperation *oper = [[[NSInvocationOperation alloc] initWithTarget:importer
                                                                   selector:@selector(importContactsFromAddressBook)
                                                                     object:nil] autorelease];
[queue addOperation:oper];
[importer release];