Address Book Authorization in iOS8
I have a problem to get the permission to access the AddressBook since I updated on iOS 8.
In iOS 7 the system asks the user whether my App is allowed to access his AddressBook but since iOS8 there haven't been any requests. Every time I check for the ABAddressBookGetAuthorizationStatus before I start my "MyGetAddressesOfContacts" function, the Status is equal to kABAuthorizationStatusNotDetermined. Like you can see here:
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No permission" message:@"This App has no permition to access your contacts. Please check it in your privacy settings of your device." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
return nil;
}
// This AlertView pops up every time!
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Why this App needs your contacts" message:@"In the following your device will ask you whether this App is allowed to access your contacts. [...]" delegate:self cancelButtonTitle:@"I understand" otherButtonTitles: nil];
[alert show];
}
After this check I am going to start the MyGetAddressesOfContacts function which looks like this:
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *contactArr = (NSArray *)CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
NSMutableDictionary *dPersons = [[NSMutableDictionary alloc] init];
for (int i = 0; i < [contactArr count]; i++)
{
ABRecordRef person = (ABRecordRef)CFBridgingRetain([contactArr objectAtIndex:i]);
NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString *sPersonName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);
NSString *sAddress;
for(CFIndex j = 0; j < ABMultiValueGetCount(address); j++)
{
CFDictionaryRef addressDict = ABMultiValueCopyValueAtIndex(address, j);
CFStringRef streetValue = CFDictionaryGetValue(addressDict, kABPersonAddressStreetKey);
CFStringRef cityValue = CFDictionaryGetValue(addressDict, kABPersonAddressCityKey);
CFStringRef countryValue = CFDictionaryGetValue(addressDict, kABPersonAddressCountryKey);
sAddress = [NSString stringWithFormat:@"%@ %@, %@", streetValue, cityValue, countryValue];
[dPersons setObject:sAddress forKey: [NSString stringWithFormat:@"%@%d %ld", @"AddressFromNameID", i, j]];
}
[dPersons setObject:sPersonName forKey: [NSString stringWithFormat:@"%@ %d", @"NameWithID", i]];
}
return dPersons;
Hope someone can help.
After you created AB ...
ABAddressBookRef addressBook = ABAddressBookCreate();
call the following ...
ABAddressBookRequestAccessWithCompletion(addressBook, nil);
This will display prompt for user to allow your application access "contacts"; As the second parameter you may get result callback and behave accordingly. Please refer to Apple documentation
链接地址: http://www.djcxy.com/p/22802.html上一篇: 小矩阵比HMatrix快100倍?
下一篇: iOS8中的地址簿授权