ios 获取通讯录 检测通讯录权限

- (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 获取通讯录 检测通讯录权限)