iOS 获取手机通讯录遍历时,只有一个联系人(获取到了所有联系人,可是遍历时出问题了)求大神帮忙

 #import "AddressBook.h"
#import "pinyin.h"//将汉字转换成英文

#import "Person.h"//将联系人信息存储成一个一个的person

//存储通讯录的类
@interface AddressBook ()


@end

@implementation AddressBook

static AddressBook *helper = nil;
+ (AddressBook *)sharedContactHelper {
    @synchronized(self) {
        if (helper == nil) {
            helper = [[AddressBook alloc] init];
            [helper requestAddressBook];//读取数据
        }
    }
    return helper;
}
- (NSMutableDictionary *)dic {
    if (!_dic) {
        self.dic =  [NSMutableDictionary dictionaryWithCapacity:1];
    }
    return _dic;
}
- (NSMutableArray *)tempArr {
    if (!_tempArr) {
        self.tempArr = [NSMutableArray arrayWithCapacity:1];
    }
    return _tempArr;
}
//请求访问通讯录
- (void)requestAddressBook {
    //新建一个通讯录类
    self.addressBooks = nil;
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
        self.addressBooks = ABAddressBookCreateWithOptions(NULL, NULL);
        //GCD 信号量控制并发
//        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
        ABAddressBookRequestAccessWithCompletion(_addressBooks, ^(bool granted, CFErrorRef error) {
            if (!granted) {
                NSLog(@"未获得通讯录访问权限");
            }
            [self initAllPerson];//取得所有通讯录记录
//            dispatch_semaphore_signal(sema);
        });
//        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

    } else {
        _addressBooks = ABAddressBookCreate();
    }
}
 //取得所有通讯录记录
- (void)initAllPerson {

    //取得通讯录访问授权
    ABAuthorizationStatus authorization = ABAddressBookGetAuthorizationStatus();
    //如果未获得授权
    if (authorization != kABAuthorizationStatusAuthorized) {
        NSLog(@"尚未获得通讯录访问授权");
        return;
    }
    //取得通讯录中的所有人
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(self.addressBooks);
    self.temPeoples = [NSMutableArray arrayWithCapacity:1];
    self.temPeoples = (__bridge NSMutableArray *)allPeople;

    //通讯录中人数
    CFIndex numPeople = ABAddressBookGetPersonCount(_addressBooks);

    self.allPerson = [NSMutableArray arrayWithCapacity:1];//存储过滤后的联系人
    self.dataSourse = [NSMutableDictionary dictionaryWithCapacity:1];//存储有联系人的分区信息.
    self.tempDic = [NSMutableDictionary dictionaryWithCapacity:1];//存储各个A~Z分区对应联系人的信息.

    NSMutableArray *phoneArray = [NSMutableArray arrayWithCapacity:1];//联系人可能有多个手机号
    self.keyArray = [[NSMutableArray alloc] initWithCapacity:1];//存储姓名首字母
    for (int i = 0; i < numPeople; i++) {
        ABRecordRef people = CFArrayGetValueAtIndex(allPeople, i);
//        CFTypeRef abFirstName = ABRecordCopyValue((__bridge ABRecordRef)(_temPeoples[i]), kABPersonFirstNameProperty);//获取联系人的名字
//        CFTypeRef abLastName = ABRecordCopyValue((__bridge ABRecordRef)(_temPeoples[i]), kABPersonLastNameProperty);//获取联系人的姓
//        //        CFTypeRef abFullName = ABRecordCopyCompositeName((__bridge ABRecordRef)(temPeoples[i]));//获取联系人完整的姓名。
        NSString *abFirstName = (__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));//名字
        NSString *abLastName = (__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty));//姓氏
        NSString *nameString = [[NSString alloc] init];
        //判断姓名
        //        if ((__bridge id) abFullName != nil) {
        //            nameString = (__bridge NSString *)abFullName;
        //        } else {
        if (abLastName.length > 0) {
            nameString = [NSString stringWithFormat:@"%@%@", abLastName,abFirstName];
        } else {
            nameString = [NSString stringWithFormat:@"%@", abFirstName];
        }
        //        }
        helper.name = nameString;//姓名
        NSLog(@"name: %@", helper.name);
        //获取汉字姓名的首字母,并变成大写
        NSString *firstWord = [[NSString stringWithFormat:@"%c", pinyinFirstLetter([nameString characterAtIndex:0])] uppercaseString];
        //判断手机号
        ABMultiValueRef phones = ABRecordCopyValue(people, kABPersonPhoneProperty);
        for (NSInteger j = 0; j < ABMultiValueGetCount(phones); j++) {
            [phoneArray addObject:(__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, j))];
        }
//        phoneArray = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(phones));
                NSLog(@"phoneArray = %@", phoneArray);
        //        long count = ABMultiValueGetCount(phone);//单个联系人的手机号码数量
        if (phoneArray.count > 0) {
            for (int index = 0; index < phoneArray.count; index++) {
                NSString *phoneNumber = [phoneArray objectAtIndex:index];
                NSString *phoneNumberLabel = (__bridge NSString *)(ABMultiValueCopyLabelAtIndex(phones, index));
                if ([self isMobileNumber:phoneNumberLabel]) {//判断是否为移动号码
                    helper.telePhone = phoneNumber;
                    NSLog(@"phoneNumber = %@", phoneNumberLabel);
                }
                //判断名字和号码是否为空
                if ( !helper.telePhone.length || !helper.name.length) {
                    return;
                } else {
                    [self.dic setValue:[NSString stringWithFormat:@"%@", helper.name] forKey:@"name"];
                    [self.dic setValue:[NSString stringWithFormat:@"%@", helper.telePhone] forKey:@"phone"];
                    [_keyArray addObject:firstWord];//添加首字母
                    [self.allPerson addObject:_dic];
                }
            }
        }
        NSLog(@"dic : %@", self.dic);
        CFRelease(people);
        CFRelease(phones);
    }
    NSLog(@"allPerson : %@", self.allPerson);
    CFRelease(allPeople);
    [self getData];


}

static AddressBook *helper = nil;

  • (AddressBook *)sharedContactHelper { @synchronized(self) { if (helper == nil) { helper = [[AddressBook alloc] init]; [helper requestAddressBook];//读取数据 } } return helper; }
  • (NSMutableDictionary *)dic { if (!_dic) { self.dic = [NSMutableDictionary dictionaryWithCapacity:1]; } return _dic; }
  • (NSMutableArray *)tempArr { if (!_tempArr) { self.tempArr = [NSMutableArray arrayWithCapacity:1]; } return _tempArr; } //请求访问通讯录
  • (void)requestAddressBook {
    //新建一个通讯录类
    self.addressBooks = nil;
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
    self.addressBooks = ABAddressBookCreateWithOptions(NULL, NULL);
    //GCD 信号量控制并发
    // dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    ABAddressBookRequestAccessWithCompletion(_addressBooks, ^(bool granted, CFErrorRef error) {
    if (!granted) {
    NSLog(@"未获得通讯录访问权限");
    }
    [self initAllPerson];//取得所有通讯录记录
    // dispatch_semaphore_signal(sema);
    });
    // dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

    } else {
    _addressBooks = ABAddressBookCreate();
    }
    }

//取得所有通讯录记录

  • (void)initAllPerson {

    //取得通讯录访问授权
    ABAuthorizationStatus authorization = ABAddressBookGetAuthorizationStatus();
    //如果未获得授权
    if (authorization != kABAuthorizationStatusAuthorized) {
    NSLog(@"尚未获得通讯录访问授权");
    return;
    }
    //取得通讯录中的所有人
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(self.addressBooks);
    self.temPeoples = [NSMutableArray arrayWithCapacity:1];
    self.temPeoples = (__bridge NSMutableArray *)allPeople;

    //通讯录中人数
    CFIndex numPeople = ABAddressBookGetPersonCount(_addressBooks);

    self.allPerson = [NSMutableArray arrayWithCapacity:1];//存储过滤后的联系人
    self.dataSourse = [NSMutableDictionary dictionaryWithCapacity:1];//存储有联系人的分区信息.
    self.tempDic = [NSMutableDictionary dictionaryWithCapacity:1];//存储各个A~Z分区对应联系人的信息.

    NSMutableArray *phoneArray = [NSMutableArray arrayWithCapacity:1];//联系人可能有多个手机号
    self.keyArray = [[NSMutableArray alloc] initWithCapacity:1];//存储姓名首字母
    for (int i = 0; i < numPeople; i++) {
    ABRecordRef people = CFArrayGetValueAtIndex(allPeople, i);
    // CFTypeRef abFirstName = ABRecordCopyValue((__bridge ABRecordRef)(_temPeoples[i]), kABPersonFirstNameProperty);//获取联系人的名字
    // CFTypeRef abLastName = ABRecordCopyValue((__bridge ABRecordRef)(_temPeoples[i]), kABPersonLastNameProperty);//获取联系人的姓
    // // CFTypeRef abFullName = ABRecordCopyCompositeName((__bridge ABRecordRef)(temPeoples[i]));//获取联系人完整的姓名。
    NSString *abFirstName = (__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));//名字
    NSString *abLastName = (__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty));//姓氏
    NSString *nameString = [[NSString alloc] init];
    //判断姓名
    // if ((__bridge id) abFullName != nil) {
    // nameString = (__bridge NSString *)abFullName;
    // } else {
    if (abLastName.length > 0) {
    nameString = [NSString stringWithFormat:@"%@%@", abLastName,abFirstName];
    } else {
    nameString = [NSString stringWithFormat:@"%@", abFirstName];
    }
    // }
    helper.name = nameString;//姓名
    NSLog(@"name: %@", helper.name);
    //获取汉字姓名的首字母,并变成大写
    NSString *firstWord = [[NSString stringWithFormat:@"%c", pinyinFirstLetter([nameString characterAtIndex:0])] uppercaseString];
    //判断手机号
    ABMultiValueRef phones = ABRecordCopyValue(people, kABPersonPhoneProperty);
    for (NSInteger j = 0; j < ABMultiValueGetCount(phones); j++) {
    [phoneArray addObject:(__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, j))];
    }
    // phoneArray = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(phones));
    NSLog(@"phoneArray = %@", phoneArray);
    // long count = ABMultiValueGetCount(phone);//单个联系人的手机号码数量
    if (phoneArray.count > 0) {
    for (int index = 0; index < phoneArray.count; index++) {
    NSString *phoneNumber = [phoneArray objectAtIndex:index];
    NSString *phoneNumberLabel = (__bridge NSString *)(ABMultiValueCopyLabelAtIndex(phones, index));
    if ([self isMobileNumber:phoneNumberLabel]) {//判断是否为移动号码
    helper.telePhone = phoneNumber;
    NSLog(@"phoneNumber = %@", phoneNumberLabel);
    }
    //判断名字和号码是否为空
    if ( !helper.telePhone.length || !helper.name.length) {
    return;
    } else {
    [self.dic setValue:[NSString stringWithFormat:@"%@", helper.name] forKey:@"name"];
    [self.dic setValue:[NSString stringWithFormat:@"%@", helper.telePhone] forKey:@"phone"];
    [_keyArray addObject:firstWord];//添加首字母
    [self.allPerson addObject:_dic];
    }
    }
    }
    NSLog(@"dic : %@", self.dic);
    CFRelease(people);
    CFRelease(phones);
    }
    NSLog(@"allPerson : %@", self.allPerson);
    CFRelease(allPeople);
    [self getData];

}

打印出allPeople控制台输出了所有联系人信息, 可是打印allPerson时,控制台只打印了一个联系人的信息.

  • 什么情况, 前面的+ - 号, 等号去哪里了??

    static AddressBook *helper = nil;
    + (AddressBook *)sharedContactHelper {
    @synchronized(self) {
        if (helper == nil) {
            helper = [[AddressBook alloc] init];
            [helper requestAddressBook];//读取数据
        }
    }
    return helper;
    }
    - (NSMutableDictionary *)dic {
    if (!_dic) {
        self.dic =  [NSMutableDictionary dictionaryWithCapacity:1];
    }
    return _dic;
    }
    - (NSMutableArray *)tempArr {
    if (!_tempArr) {
        self.tempArr = [NSMutableArray arrayWithCapacity:1];
    }
    return _tempArr;
    }
    //请求访问通讯录
    - (void)requestAddressBook {
    //新建一个通讯录类
    self.addressBooks = nil;
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
        self.addressBooks = ABAddressBookCreateWithOptions(NULL, NULL);
        //GCD 信号量控制并发
    //        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
        ABAddressBookRequestAccessWithCompletion(_addressBooks, ^(bool granted, CFErrorRef error) {
            if (!granted) {
                NSLog(@"未获得通讯录访问权限");
            }
            [self initAllPerson];//取得所有通讯录记录
    //            dispatch_semaphore_signal(sema);
        });
    //        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    
    } else {
        _addressBooks = ABAddressBookCreate();
    }
    }
    

//取得所有通讯录记录

  • (void)initAllPerson {

    //取得通讯录访问授权
    ABAuthorizationStatus authorization = ABAddressBookGetAuthorizationStatus();
    //如果未获得授权
    if (authorization != kABAuthorizationStatusAuthorized) {
    NSLog(@"尚未获得通讯录访问授权");
    return;
    }
    //取得通讯录中的所有人
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(self.addressBooks);
    self.temPeoples = [NSMutableArray arrayWithCapacity:1];
    self.temPeoples = (__bridge NSMutableArray *)allPeople;

    //通讯录中人数
    CFIndex numPeople = ABAddressBookGetPersonCount(_addressBooks);

    self.allPerson = [NSMutableArray arrayWithCapacity:1];//存储过滤后的联系人
    self.dataSourse = [NSMutableDictionary dictionaryWithCapacity:1];//存储有联系人的分区信息.
    self.tempDic = [NSMutableDictionary dictionaryWithCapacity:1];//存储各个A~Z分区对应联系人的信息.

    NSMutableArray *phoneArray = [NSMutableArray arrayWithCapacity:1];//联系人可能有多个手机号
    self.keyArray = [[NSMutableArray alloc] initWithCapacity:1];//存储姓名首字母
    for (int i = 0; i < numPeople; i++) {
    ABRecordRef people = CFArrayGetValueAtIndex(allPeople, i);
    // CFTypeRef abFirstName = ABRecordCopyValue((__bridge ABRecordRef)(_temPeoples[i]), kABPersonFirstNameProperty);//获取联系人的名字
    // CFTypeRef abLastName = ABRecordCopyValue((__bridge ABRecordRef)(_temPeoples[i]), kABPersonLastNameProperty);//获取联系人的姓
    // // CFTypeRef abFullName = ABRecordCopyCompositeName((__bridge ABRecordRef)(temPeoples[i]));//获取联系人完整的姓名。
    NSString *abFirstName = (__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));//名字
    NSString *abLastName = (__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty));//姓氏
    NSString *nameString = [[NSString alloc] init];
    //判断姓名
    // if ((__bridge id) abFullName != nil) {
    // nameString = (__bridge NSString *)abFullName;
    // } else {
    if (abLastName.length > 0) {
    nameString = [NSString stringWithFormat:@"%@%@", abLastName,abFirstName];
    } else {
    nameString = [NSString stringWithFormat:@"%@", abFirstName];
    }
    // }
    helper.name = nameString;//姓名
    NSLog(@"name: %@", helper.name);
    //获取汉字姓名的首字母,并变成大写
    NSString *firstWord = [[NSString stringWithFormat:@"%c", pinyinFirstLetter([nameString characterAtIndex:0])] uppercaseString];
    //判断手机号
    ABMultiValueRef phones = ABRecordCopyValue(people, kABPersonPhoneProperty);
    for (NSInteger j = 0; j < ABMultiValueGetCount(phones); j++) {
    [phoneArray addObject:(__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, j))];
    }
    // phoneArray = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(phones));
    NSLog(@"phoneArray = %@", phoneArray);
    // long count = ABMultiValueGetCount(phone);//单个联系人的手机号码数量
    if (phoneArray.count > 0) {
    for (int index = 0; index < phoneArray.count; index++) {
    NSString *phoneNumber = [phoneArray objectAtIndex:index];
    NSString *phoneNumberLabel = (__bridge NSString *)(ABMultiValueCopyLabelAtIndex(phones, index));
    if ([self isMobileNumber:phoneNumberLabel]) {//判断是否为移动号码
    helper.telePhone = phoneNumber;
    NSLog(@"phoneNumber = %@", phoneNumberLabel);
    }
    //判断名字和号码是否为空
    if ( !helper.telePhone.length || !helper.name.length) {
    return;
    } else {
    [self.dic setValue:[NSString stringWithFormat:@"%@", helper.name] forKey:@"name"];
    [self.dic setValue:[NSString stringWithFormat:@"%@", helper.telePhone] forKey:@"phone"];
    [_keyArray addObject:firstWord];//添加首字母
    [self.allPerson addObject:_dic];
    }
    }
    }
    NSLog(@"dic : %@", self.dic);
    CFRelease(people);
    CFRelease(phones);
    }
    NSLog(@"allPerson : %@", self.allPerson);
    CFRelease(allPeople);
    [self getData];

}


控制台打印了所有联系人的信息, 可是我要遍历联系人,过滤掉不是移动的号码, 可是每次控制台输出都是一个联系人,而且还是同一个联系人信息. 代码比较乱,注释掉得代码没有删除,估计很费劲, 不过求大神帮忙啊~