2

我在 iPhone 5.1 模拟器或更低版本中获取地址簿时遇到了一些问题,此代码在 iPhone 6 模拟器中运行良好。我在一些参考资料中发现只有 ios 6 需要权限,但我的情况不同。通讯录总是不返回联系方式和。有没有人告诉我我需要在项目中做什么或配置。提前谢谢了。

ABAddressBookRef addressBook = ABAddressBookCreate();





CFArrayRef arrRefPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);


CFMutableArrayRef mArrRefPeople = CFArrayCreateMutableCopy(kCFAllocatorDefault, CFArrayGetCount(arrRefPeople), arrRefPeople);
CFRelease(arrRefPeople);


CFArraySortValues(mArrRefPeople, CFRangeMake(0, CFArrayGetCount(mArrRefPeople)), (CFComparatorFunction)ABPersonComparePeopleByName, (void *)ABPersonGetSortOrdering());


NSArray *arrChosung = [[NSArray alloc] initWithObjects:@"ㄱ",@"ㄲ",@"ㄴ",@"ㄷ",@"ㄸ",@"ㄹ",@"ㅁ",@"ㅂ",@"ㅃ",@"ㅅ",@" ㅆ",@"ㅇ",@"ㅈ",@"ㅉ",@"ㅊ",@"ㅋ",@"ㅌ",@"ㅍ",@"ㅎ", @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];
NSMutableArray *object = [[NSMutableArray alloc] init];
NSMutableArray *section = [[NSMutableArray alloc] init];


int lengthOfPeople = (int)CFArrayGetCount(mArrRefPeople);
    NSLog(@"Length of people %d",lengthOfPeople);

int chosungIndex = -1;
int j=-1;

for(int i=0; i<lengthOfPeople; i++) {
    ABRecordRef person = CFArrayGetValueAtIndex(mArrRefPeople, i);

    NSMutableDictionary *dicAddress = [[NSMutableDictionary alloc] init];

    NSNumber *numPersonId = [NSNumber numberWithInt:(int)ABRecordGetRecordID(person)];

    NSString *strName = (NSString *)ABRecordCopyCompositeName(person);
    ABMultiValueRef refEmail = ABRecordCopyValue(person, kABPersonEmailProperty);
    if (refEmail) {
        if (ABMultiValueGetCount(refEmail) > 0) {
            CFStringRef email = ABMultiValueCopyValueAtIndex(refEmail, 0);

            [dicAddress setObject:email ? (NSString *)email : @"" forKey:@"email"];
            if (nil != email) {
                CFRelease(email);
            }

        }
        CFRelease(refEmail);
    }
    if (nil == [dicAddress objectForKey:@"email"]) {
        [dicAddress setObject:@"" forKey:@"email"];
    }

    NSString *strNote = (NSString *)ABRecordCopyValue(person, kABPersonNoteProperty);

    [dicAddress setObject:strNote ? strNote : @"" forKey:@"memo"];
    [strNote release];


    //NSLog(@"%d", [numPersonId integerValue]);
    [dicAddress setObject:numPersonId forKey:@"id"];
    [dicAddress setObject:strName forKey:@"name"];
    [strName release];

    ABMultiValueRef addressValues = ABRecordCopyValue(person, kABPersonPhoneProperty);
    ABMultiValueRef numbers = ABMultiValueCopyArrayOfAllValues(addressValues);
    CFRelease(addressValues);

    NSArray *arrPhone = (NSArray *)numbers;

    if (nil != [arrPhone objectAtIndex:0]) {
        [dicAddress setObject:[arrPhone objectAtIndex:0] forKey:@"hp"];
    } else {
        [dicAddress setObject:@"" forKey:@"hp"];
    }

    [object addObject:dicAddress];
    [dicAddress release];


    if([self chosungIndexWithString:strName] != chosungIndex) {
        chosungIndex = (int)[self chosungIndexWithString:strName];
        if (-1 == j) {
            NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:[arrChosung objectAtIndex:chosungIndex], @"chosung", [NSNumber numberWithInt:i+1], @"row", nil];
            [section addObject:dic];
            [dic release];
        } else {
            int prevRow = [[[section objectAtIndex:j] objectForKey:@"row"] intValue];
            NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:[arrChosung objectAtIndex:chosungIndex], @"chosung", [NSNumber numberWithInt:i+1-prevRow], @"row", nil];
            [section addObject:dic];
            [dic release];
        }
        j++;
    }
    [arrPhone release];

}
CFRelease(mArrRefPeople);

_lengthOfPeople = lengthOfPeople;
    NSLog(@"Length of people %d",lengthOfPeople);
_arrChosung = arrChosung;

_object = object;

_section = section;

CFRelease(addressBook);

     NSLog(@"Completed getting address");
4

1 回答 1

2

我会发布一个官方答案,因为这可能有助于在未来节省一些时间。

这里的问题原来是每个不同的模拟器都没有添加联系人。每个版本的 iOS 都有一个单独的模拟器环境。这意味着每个人都有自己的一组应用程序、自己的一组照片和联系人以及自己的一组设置。

已针对 iOS 6.0 模拟器进行了测试。这个模拟器已经填充了联系人。当测试转移到 iOS 5.0 或 5.1 时,一个全新的模拟器正在运行,没有人注意到这个模拟器没有添加任何联系人。假设可以使用相同的联系人。但他们不是。

于 2012-11-12T03:13:54.347 回答