sinat_30668065 2015-09-10 01:20 采纳率: 33.3%
浏览 1675

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];


}

  • 写回答

3条回答 默认 最新

  • sinat_30668065 2015-09-10 02:02
    关注

    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时,控制台只打印了一个联系人的信息.

    评论

报告相同问题?

悬赏问题

  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。