在IOS中如何使用ABaddressbook仅获取本地联系人(存储在iphone而非邮件同步联系人中)

时间:2015-10-13 03:07:44

标签: ios

我试图取消联系

它返回完整的联系人列表,我需要过滤本地联系人

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

    contactList = [[NSMutableArray alloc] init];
    tableSectionTitles = [[NSMutableArray alloc]init];
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);


//    CFArrayRef groups = ABAddressBookCopyArrayOfAllGroupsInSource(addressBook);

    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
    NSLog(@"nPeople-----%ld",nPeople);

    for (int i=0;i < nPeople;i++) {
        NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];
        NSMutableDictionary *keyDic=[NSMutableDictionary dictionary];


        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
        NSString *mobileNo = [[NSString alloc] init];
        //For username and surname
        ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));


        ABMultiValueRef phoneNumbers = ABRecordCopyValue(ref, kABPersonPhoneProperty);
        if (phoneNumbers) {
            CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
            for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
                CFStringRef label = ABMultiValueCopyLabelAtIndex(phoneNumbers, i);

                NSLog(@"Label value------%@",label);
                if (label) {

                    if (CFEqual(label, kABHomeLabel)) {
                        // it's the user's work phone /
                        [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];


                    } else if (CFEqual(label, kABPersonPhoneMobileLabel)) {
                        // it's the user's home phone /
                        [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];

                    } else if (CFEqual(label, kABPersonPhoneIPhoneLabel)) {
                        // other specific cases of your choosing... /
                        [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];

                    }else if (CFEqual(label, kABWorkLabel)) {
                        // other specific cases of your choosing... /
                        [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];

                    } else {
                        // it's some other label, such as a custom label /
                        // [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];
                        [dOfPerson setObject:@"" forKey:@"Phone"];

                    }
                    CFRelease(label);
                }
            }
            CFRelease(phoneNumbers);
        } else {

            [dOfPerson setObject:@"" forKey:@"Phone"];


        }

        // CFStringRef firstName, lastName;
        NSString *firstName;
        NSString *lastName;

        firstName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonFirstNameProperty));
        lastName  = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonLastNameProperty));


        /*  if(!firstName) {

         firstName = @"N";
         } */

        if(!firstName  && !lastName) {

            firstName = @"No Name";
            lastName = @"";
        }

        if(!lastName ) {

            lastName = @"";

        }

        [dOfPerson setObject:[NSString stringWithFormat:@"%@ %@", firstName, lastName] forKey:@"name"];

        //For Email ids

        ABMutableMultiValueRef eMail  = ABRecordCopyValue(ref, kABPersonEmailProperty);
        if(ABMultiValueGetCount(eMail) > 0) {
            [dOfPerson setObject:(__bridge NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:@"email"];

        }


        NSString *str = [[NSString stringWithFormat:@"%@", firstName]uppercaseString];

        NSLog(@"str title1111------%@",str);



        //IMAGE Data
        UIImage* image;

        if(ABPersonHasImageData(ref)){
            NSLog(@"imagedata--------%@",[UIImage imageWithData:(__bridge NSData *)ABPersonCopyImageData(ref)]);
            image = [UIImage imageWithData:(__bridge NSData *)ABPersonCopyImageData(ref)];
            [dOfPerson setObject:image forKey:@"image"];

        }else{
            //image = Nil;
            [dOfPerson setObject:@"ok3.png" forKey:@"image"];
        }



        if ([contactList count]<100) { // Final option

            NSCharacterSet *s = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
            s = [s invertedSet];

            if([str length]>0) {


                NSRange r = [[str substringToIndex:1] rangeOfCharacterFromSet:s];

                if (r.location != NSNotFound) {
                    NSLog(@"the string contains illegal characters");


                }else{


                    if(![tableSectionTitles containsObject:[[str substringToIndex:1] uppercaseString]]) {


                        [tableSectionTitles addObject:[[str substringToIndex:1] uppercaseString]];
                        [tableSectionTitles sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
                        NSLog(@"Section title222------%@",tableSectionTitles);

                    }


                    [keyDic setObject:dOfPerson forKey:[[str substringToIndex:1]uppercaseString]];
                    [contactList addObject:keyDic];

1 个答案:

答案 0 :(得分:0)

SDK不提供正式的方式来查询属于该用户的地址簿中的联系人。 This has been asked before