作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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/
我是一名优秀的程序员,十分优秀!