2

I get some problems with get address book in iPhone 5.1 simulator or lower, this code works well in iPhone 6 simulator. I found in some references that only ios 6 need permission, but my case is different. Address book always returns no contact and. Does anybody tell me what need I do or configurate in the project. Many thanks in advance.

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");
Linh Nguyen
  • 925
  • 1
  • 10
  • 23
  • 1
    What is the error? Which line of code is causing the error? – rmaddy Nov 12 '12 at 02:31
  • The code always return 0 contact. So I cannot get contact information for future use – Linh Nguyen Nov 12 '12 at 02:38
  • So there is no error? The code simply doesn't do what you expect regardless of which version of iOS you are using? Have you used the debugger to step through the code to figure out where things go wrong? – rmaddy Nov 12 '12 at 02:40
  • Yes, I did, but I don't know why it doesn't work for ios 5 or 4.3. I saw in this site usually it need permission for ios 6 only. I also ask for permission but the variable lengthOfPeople always returns 0 (contact book having a lot of contact). Do I need change some configuration ? – Linh Nguyen Nov 12 '12 at 02:44
  • 1
    In iOS 4 or 5 there is nothing extra to do. What is the count of `arrRefPeople`? – rmaddy Nov 12 '12 at 02:47
  • The problem seems being solved after I use CFArrayGetCount(arrRefPeople) but only for ios 5.1. I don't know why. Thank you so much ^^. It still doesn't work for ios 5 or 4.3 – Linh Nguyen Nov 12 '12 at 02:56
  • It returns 0 in ios 5 or 4.3. – Linh Nguyen Nov 12 '12 at 03:01
  • 1
    Here's a silly question. Are there any contacts in the 4.3 or 5.x simulator? Pull up the Contacts app and look. There is a separate copy of the simulator for each version of iOS. Each has its own set of contacts. – rmaddy Nov 12 '12 at 03:04

1 Answers1

2

I'll post an official answer since this might help save someone some time in the future.

The problem here turned out to be that there were no contacts added to each of the different simulators. There is a separate simulator environment for each version of iOS. This means that each one has its own set of apps, its own set of photos and contacts, and its own set of settings.

Testing had been done against the iOS 6.0 simulator. This simulator had been populated with contacts. When testing moved to iOS 5.0 or 5.1, a whole new simulator was being run and no one noticed that no contacts had been added to this simulator. It was assumed the same contacts would be available. But they are not.

rmaddy
  • 314,917
  • 42
  • 532
  • 579