I'm reading all the phone-numbers from the AdressBook api, and in some cases i've got a format like this: "(06)\\U00a057\\U00a011\\U00a042\\U00a095"
When checking my own addressbook however, it gives me a regular format like 0624567899
How come and how do I solve this?
I had the same problem, saw your post while searching for a solution, then i thought that maybe i should unpack the array instead of just NSLog(@"Phone = %@", self.array). Therefore you have to unpack or iterate through your array and your values will show correctly:
for (NSString *d in self.array) {
NSLog(@"Phone = %@", d);
)
I order to fetch phone numbers or email from the Address book try this
-(NSArray*)fetchAddressBook
{
NSMutableArray* arrAddressBook= [[NSMutableArray alloc] init];
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kContactsAccessPermission];
[[NSUserDefaults standardUserDefaults] synchronize];
CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
NSArray *allPeople = (__bridge NSArray *)(ABAddressBookCopyArrayOfAllPeople(addressBook));
NSString *name = @"";
NSString *firstname = @"";
NSString *lastname = @"";
for (id person in allPeople)
{
firstname = (__bridge NSString *)ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonFirstNameProperty);
lastname = (__bridge NSString *)ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonLastNameProperty);
if (!lastname) {
lastname = @"";
}
if (!firstname) {
firstname = @"";
}
name = [NSString stringWithFormat:@"%@ %@",firstname,lastname];
NSData *data = [name dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
name = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
ABMultiValueRef Phone = ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonPhoneProperty);
ABMultiValueRef Email = ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonEmailProperty);
NSMutableDictionary *aTemp=[[NSMutableDictionary alloc]init];
NSMutableArray *arrPhone=[[NSMutableArray alloc]init];
for (CFIndex i = 0; i < ABMultiValueGetCount(Phone); i++) {
NSString *phoneNumber = [self removeSpecialCheractersFromPhoneNumber:(__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(Phone, i)];
[arrPhone addObject:phoneNumber];
}
NSMutableArray *arrEmail=[[NSMutableArray alloc]init];
for (CFIndex i = 0; i < ABMultiValueGetCount(Email); i++) {
NSString *phoneNumber;
phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(Email, i);
[arrEmail addObject:phoneNumber];
}
if ([arrPhone count] == 0) {
[arrPhone addObject:@""];
}else if ([firstname isEqualToString:@""] && [lastname isEqualToString:@""]) {
name = [arrPhone firstObject];
}
[aTemp setObject:arrPhone forKey:@"phone"];
if ([arrEmail count] == 0) {
[arrEmail addObject:@""];
}
[aTemp setObject:arrEmail forKey:@"email"];
if (name) {
[aTemp setObject:name forKey:@"name"];
}else{
[aTemp setObject:@"" forKey:@"name"];
}
[arrAddressBook addObject:aTemp];
CFRelease(Phone);
CFRelease(Email);
}
return arrAddressBook;
}
else {
// Send an alert telling user to change privacy setting in settings app
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:kContactsAccessPermission];
[[NSUserDefaults standardUserDefaults] synchronize];
}
return nil;
}
The above function uses another function "removeSpecialCheractersFromPhoneNumber". This
//This method removes any type of special character from the phone numbers
-(NSString *) removeSpecialCheractersFromPhoneNumber:(NSString *)phoneNumber
{
NSMutableString *result = [NSMutableString stringWithCapacity:phoneNumber.length];
NSScanner *scanner = [NSScanner scannerWithString:phoneNumber];
NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
while ([scanner isAtEnd] == NO)
{
NSString *buffer;
if ([scanner scanCharactersFromSet:numbers intoString:&buffer])
{
[result appendString:buffer];
}
else
{
[scanner setScanLocation:([scanner scanLocation] + 1)];
}
}
return result;
}
Hope this will help you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.