iphone - 获取地址簿 ios 5、ios 4.3 错误

标签 iphone objective-c ios-simulator abaddressbook

我在 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");

最佳答案

我将发布正式答案,因为这可能有助于在将来节省某人的时间。

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

测试已针对 iOS 6.0 模拟器进行。这个模拟器里已经充满了联系人。当测试转移到 iOS 5.0 或 5.1 时,一个全新的模拟器正在运行,没有人注意到没有联系人被添加到这个模拟器中。假设有相同的联系人可用。但事实并非如此。

关于iphone - 获取地址簿 ios 5、ios 4.3 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13337627/

相关文章:

iphone - 为什么第二次崩溃?

iphone - iOS 在 iOS 6 中隐藏标签栏会创建黑条(针对 iOS 6 的修复破坏了 iOS 7!)

objective-c - 如何确保 Cocoa 中的窗口只显示一次?

ios-simulator - 为什么 iOS 模拟器无法运行?

ios - [sender.date descriptionWithLocale :[NSLocale currentLocale]] gives different result?

iphone - 自定义 NavigationBar 按钮在 iOS 7 中看起来不同

iphone - 如何在按下 UIBarButtonItem 之前启用/禁用方向(旋转)?

iphone - 将 dispatch_async 与核心数据一起使用时获取 EXC_BAD_ACCESS

iphone - 如何知道 UITextView 是否有焦点

objective-c - sqlite 适用于模拟器,但不适用于设备