RainShin 2015-09-10 04:10 采纳率: 0%
浏览 1611

iOS 获取通讯录后,可打印 输出结果只有一个联系人信息.

打印self.allPerson可以打印出所有联系人信息, 可是打印self.allPeople时,什么结果也没有输出.?????

 #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 (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))];
        }
          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];
                }
            }
        }

        CFRelease(people);
        CFRelease(phones);
    }
    NSLog(@"allPerson : %@", self.allPerson);
    CFRelease(allPeople);
    [self getData];


}
  • 写回答

1条回答 默认 最新

  • RainShin 2015-09-10 05:13
    关注

    没有大神可以解决?

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记