- (void)buttonClick
{
[selfCheckAddressBookAuthorization:^(boolisAuthorized){
if(isAuthorized) {
}else{
}
}];
}
- (void)loadPerson
{
ABAddressBookRefaddressBookRef =ABAddressBookCreateWithOptions(NULL,NULL);
if(ABAddressBookGetAuthorizationStatus() ==kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(boolgranted,CFErrorReferror){
if(granted) {
ABAddressBookRefaddressBook =ABAddressBookCreateWithOptions(NULL,nil);
//授权成功
[selfremovePlaceholderView];
//设置空态页
[selfsetupLoadingView];
//获取联系人
[selfcopyAddressBook:addressBook];
}else{
[SVProgressHUDshowImage:nilstatus:@"没有获取通讯录权限"];
}
});
}elseif(ABAddressBookGetAuthorizationStatus() ==kABAuthorizationStatusAuthorized){
//有权限
CFErrorRef*error =NULL;
ABAddressBookRefaddressBook =ABAddressBookCreateWithOptions(NULL, error);
[selfremovePlaceholderView];
[selfsetupLoadingView];
[selfcopyAddressBook:addressBook];
}else{
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUDshowImage:nilstatus:@"没有获取通讯录权限"];
});
}
}
//获取联系人
- (void)copyAddressBook:(ABAddressBookRef)addressBook
{
NSMutableDictionary*dic = [NSMutableDictionarydictionary];
CFIndexnumberOfPeople =ABAddressBookGetPersonCount(addressBook);
CFArrayRefallLinkPeople =ABAddressBookCopyArrayOfAllPeople(addressBook);
for(inti =0; i < numberOfPeople; i++){
ABRecordRefperson =CFArrayGetValueAtIndex(allLinkPeople, i);
//获取当前联系人名字
NSString*firstName=(__bridgeNSString*)(ABRecordCopyValue(person,kABPersonFirstNameProperty));
//获取当前联系人姓氏
NSString*lastName=(__bridgeNSString*)(ABRecordCopyValue(person,kABPersonLastNameProperty));
//获取当前联系人的公司
NSString*organization=(__bridgeNSString*)(ABRecordCopyValue(person,kABPersonOrganizationProperty));
NSString*name = [NSStringstringWithFormat:@"%@%@",mAvailableString(lastName),mAvailableString(firstName)];
if(!name.length) {
name =mAvailableString(organization);
}
if(name) {
//读取电话多值
ABMultiValueRefphone =ABRecordCopyValue(person,kABPersonPhoneProperty);
for(intk =0; k
{
NSString* personPhone = (__bridgeNSString*)ABMultiValueCopyValueAtIndex(phone, k);
[dicsetObject:nameforKey:[personPhonestringByReplacingOccurrencesOfString:@" "withString:@""]];
}
}
}
dispatch_async(dispatch_get_main_queue(), ^{
[LYProgressHUDshowAnimationWithMaskType:LYProgressHUDHUDMaskTypeClear];
//把通讯录上传给服务器
}failtureBlock:^(NSError*error) {
[LYProgressHUDdismiss];
- (void)CheckAddressBookAuthorization:(void(^)(boolisAuthorized))block
{
ABAddressBookRefaddressBook =ABAddressBookCreateWithOptions(NULL,NULL);
ABAuthorizationStatusauthStatus =ABAddressBookGetAuthorizationStatus();
if(authStatus !=kABAuthorizationStatusAuthorized)
{
ABAddressBookRequestAccessWithCompletion(addressBook, ^(boolgranted,CFErrorReferror)
{
dispatch_async(dispatch_get_main_queue(), ^{
if(error)
{
NSLog(@"Error: %@", (__bridgeNSError *)error);
}
elseif(!granted)
{
block(NO);
}
else
{
block(YES);
}
});
});
}
else
{
block(YES);
}
}
}];
});
} ```
ios 获取通讯录 检测通讯录权限
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 在开发中,我们可能会遇到这样的情况,用户自己关闭了所有的操作权限,以后要使用我们的App,总是得不到想要的结果。这...
- 一、为什么要获取权限 在越来越注重个人隐私的今天,用户很多情况下希望自己能完全掌握自己手机应用对媒体信息的一些访问...