应用程序未请求联系人权限以访问 iOS 9 中的联系人
App is Not asking contact permission to access contact in iOS 9
我正在使用以下代码获取 iPhone 联系人,但我的应用程序在 iOS 9 中未获得允许联系人的权限。我从堆栈中找到了这段代码,其他引用也相同。
- (void)getPersonOutOfAddressBook
{
//1
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (addressBook != nil) {
NSLog(@"Succesful.");
//2
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
//3
NSUInteger i = 0; for (i = 0; i < [allContacts count]; i++)
{
NSMutableDictionary *persiondict =[[NSMutableDictionary alloc]init] ;
// Person *person = [[Person alloc] init];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
//4
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson,
kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
// person.firstName = firstName;
// person.lastName = lastName;
// person.fullName = fullName;
[persiondict setValue:fullName forKey:@"fullName"] ;
//email
//5
ABMultiValueRef emails = ABRecordCopyValue(contactPerson, kABPersonEmailProperty);
//6
NSUInteger j = 0;
for (j = 0; j < ABMultiValueGetCount(emails); j++) {
NSString *email = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(emails, j);
if (j == 0) {
// person.homeEmail = email;
[persiondict setValue:email forKey:@"email"] ;
// NSLog(@"person.homeEmail = %@ ", person.homeEmail);
}
else if (j==1)
[persiondict setValue:email forKey:@"email"] ;
}
//7
[ArrUserOfContacts addObject:persiondict];
}
//8
CFRelease(addressBook);
} else {
//9
NSLog(@"Error reading Address Book");
}
}
我在这里找不到问题,用户如何获得访问联系人的权限。任何建议都会有所帮助。
您需要使用 ABAddressBookRequestAccessWithCompletion()
请求权限
ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) {
if (!granted){
NSLog(@"Just denied");
return;
}
NSLog(@"Just authorized");
});
ABAddressBookRequestAccessWithCompletion 在 iOS 中已弃用 9. 现在您应该使用联系人框架。这是 Objective C:
中的示例
CNContactStore * contactStore = [CNContactStore new];
[contactStore requestAccessForEntityType:entityType completionHandler:^(BOOL granted, NSError * _Nullable error) {
if(granted){
//
}
}];
在Swift 3:
CNContactStore().requestAccess(for: .contacts, completionHandler: { granted, error in
if (granted){
//
}
})
如果用户没有拒绝或批准对您应用中的联系人的权限,这只会请求权限。您不能请求已被用户拒绝的权限(至少现在在 iOS 10),您可以做的是将用户重定向到设置。
如果您想检查用户是否授予联系人权限,如果未授予权限,则显示警报以在设置中移动用户以授予权限。
然后使用下面的函数checkContactsPermission
作为:
-(void)checkContactsPermission {
//Check permission status
switch (ABAddressBookGetAuthorizationStatus()) {
case kABAuthorizationStatusAuthorized:
// Already permission given
break;
case kABAuthorizationStatusDenied:{
// Permission not given so move user in settings page to app.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert!" message:@"his app requires access to your contacts." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* SettingsButton = [UIAlertAction actionWithTitle:@"Settings"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSURL * settingsURL = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@%@",UIApplicationOpenSettingsURLString,[[NSBundle mainBundle]bundleIdentifier]]];
if (settingsURL) {
[[UIApplication sharedApplication] openURL:settingsURL];
}
}];
UIAlertAction* DeniedButton = [UIAlertAction actionWithTitle:@"Denied"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
}];
[alert addAction:SettingsButton];
[alert addAction:DeniedButton];
[self presentViewController:alert animated:YES completion:nil];
}
case kABAuthorizationStatusRestricted: {
// Permission not given so move user in settings page to app.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert!" message:@"his app requires access to your contacts." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* SettingsButton = [UIAlertAction actionWithTitle:@"Settings"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSURL * settingsURL = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@%@",UIApplicationOpenSettingsURLString,[[NSBundle mainBundle]bundleIdentifier]]];
if (settingsURL) {
[[UIApplication sharedApplication] openURL:settingsURL];
}
}];
UIAlertAction* DeniedButton = [UIAlertAction actionWithTitle:@"Denied"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
}];
[alert addAction:SettingsButton];
[alert addAction:DeniedButton];
[self presentViewController:alert animated:YES completion:nil];
}
break;
case kABAuthorizationStatusNotDetermined:
// Permission not determin. so request for permission.
ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) {
if (granted){
// Already permission given
}
});
break;
}
}
对于iOS10你可以使用联系人框架来检查权限。
我正在使用以下代码获取 iPhone 联系人,但我的应用程序在 iOS 9 中未获得允许联系人的权限。我从堆栈中找到了这段代码,其他引用也相同。
- (void)getPersonOutOfAddressBook
{
//1
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (addressBook != nil) {
NSLog(@"Succesful.");
//2
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
//3
NSUInteger i = 0; for (i = 0; i < [allContacts count]; i++)
{
NSMutableDictionary *persiondict =[[NSMutableDictionary alloc]init] ;
// Person *person = [[Person alloc] init];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
//4
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson,
kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
// person.firstName = firstName;
// person.lastName = lastName;
// person.fullName = fullName;
[persiondict setValue:fullName forKey:@"fullName"] ;
//email
//5
ABMultiValueRef emails = ABRecordCopyValue(contactPerson, kABPersonEmailProperty);
//6
NSUInteger j = 0;
for (j = 0; j < ABMultiValueGetCount(emails); j++) {
NSString *email = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(emails, j);
if (j == 0) {
// person.homeEmail = email;
[persiondict setValue:email forKey:@"email"] ;
// NSLog(@"person.homeEmail = %@ ", person.homeEmail);
}
else if (j==1)
[persiondict setValue:email forKey:@"email"] ;
}
//7
[ArrUserOfContacts addObject:persiondict];
}
//8
CFRelease(addressBook);
} else {
//9
NSLog(@"Error reading Address Book");
}
}
我在这里找不到问题,用户如何获得访问联系人的权限。任何建议都会有所帮助。
您需要使用 ABAddressBookRequestAccessWithCompletion()
ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) {
if (!granted){
NSLog(@"Just denied");
return;
}
NSLog(@"Just authorized");
});
ABAddressBookRequestAccessWithCompletion 在 iOS 中已弃用 9. 现在您应该使用联系人框架。这是 Objective C:
中的示例CNContactStore * contactStore = [CNContactStore new];
[contactStore requestAccessForEntityType:entityType completionHandler:^(BOOL granted, NSError * _Nullable error) {
if(granted){
//
}
}];
在Swift 3:
CNContactStore().requestAccess(for: .contacts, completionHandler: { granted, error in
if (granted){
//
}
})
如果用户没有拒绝或批准对您应用中的联系人的权限,这只会请求权限。您不能请求已被用户拒绝的权限(至少现在在 iOS 10),您可以做的是将用户重定向到设置。
如果您想检查用户是否授予联系人权限,如果未授予权限,则显示警报以在设置中移动用户以授予权限。
然后使用下面的函数checkContactsPermission
作为:
-(void)checkContactsPermission {
//Check permission status
switch (ABAddressBookGetAuthorizationStatus()) {
case kABAuthorizationStatusAuthorized:
// Already permission given
break;
case kABAuthorizationStatusDenied:{
// Permission not given so move user in settings page to app.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert!" message:@"his app requires access to your contacts." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* SettingsButton = [UIAlertAction actionWithTitle:@"Settings"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSURL * settingsURL = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@%@",UIApplicationOpenSettingsURLString,[[NSBundle mainBundle]bundleIdentifier]]];
if (settingsURL) {
[[UIApplication sharedApplication] openURL:settingsURL];
}
}];
UIAlertAction* DeniedButton = [UIAlertAction actionWithTitle:@"Denied"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
}];
[alert addAction:SettingsButton];
[alert addAction:DeniedButton];
[self presentViewController:alert animated:YES completion:nil];
}
case kABAuthorizationStatusRestricted: {
// Permission not given so move user in settings page to app.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert!" message:@"his app requires access to your contacts." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* SettingsButton = [UIAlertAction actionWithTitle:@"Settings"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSURL * settingsURL = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@%@",UIApplicationOpenSettingsURLString,[[NSBundle mainBundle]bundleIdentifier]]];
if (settingsURL) {
[[UIApplication sharedApplication] openURL:settingsURL];
}
}];
UIAlertAction* DeniedButton = [UIAlertAction actionWithTitle:@"Denied"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
}];
[alert addAction:SettingsButton];
[alert addAction:DeniedButton];
[self presentViewController:alert animated:YES completion:nil];
}
break;
case kABAuthorizationStatusNotDetermined:
// Permission not determin. so request for permission.
ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) {
if (granted){
// Already permission given
}
});
break;
}
}
对于iOS10你可以使用联系人框架来检查权限。