Objective c 访问电话号码并显示国家名称或国家代码

Objective c 访问电话号码并显示国家名称或国家代码,objective-c,addressbook,Objective C,Addressbook,我访问联系人并将其发送到服务器,我的问题是我是否有可能检测到联系人所属的国家? 谢谢。试试这个代码功能,你可以得到你想要的所有信息 +(NSArray *)getAllContacts { CFErrorRef *error = nil; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL); __block BOOL accessGranted = NO; if (ABAddressBookReque

我访问联系人并将其发送到服务器,我的问题是我是否有可能检测到联系人所属的国家?
谢谢。

试试这个代码功能,你可以得到你想要的所有信息

+(NSArray *)getAllContacts
{
CFErrorRef *error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        accessGranted = granted;
        dispatch_semaphore_signal(sema);
    });
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

}
else { // we're on iOS 5 or older
    accessGranted = YES;
}
if (accessGranted) {
    NSLog(@"Fetching contact info ----> ");
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
    CFIndex nPeople = CFArrayGetCount(allPeople);
    NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople];
    for (int i = 0; i < nPeople; i++)
    {
        ContactDetailModel *contacts = [[ContactDetailModel alloc] init];  // it is NsObject class. this is contained variables and array which required for get contact information. It is not necessary for you. you can maintain according your requirments. 
        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
        if((__bridge NSString*)ABRecordCopyCompositeName(person)){
            contacts.compse_name =  (__bridge NSString*)ABRecordCopyCompositeName(person);
            NSLog(@"compse_name = %@",contacts.compse_name);
        }
        if (!contacts.compse_name) {
            contacts.compse_name = @"";
        }

        //get First Name and Last Name

        if((__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty)){
            contacts.first_name = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        }
        if((__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty)){
            contacts.last_name =  (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
        }
        if (!contacts.first_name) {
            contacts.first_name = @"";
        }
        if (!contacts.last_name) {
            contacts.last_name = @"";
        }
        NSLog(@"fname and lname = %@ %@",contacts.first_name,contacts.last_name);


        /// URL
        ABMultiValueRef webpages = ABRecordCopyValue(person, kABPersonURLProperty);
        NSString* homepage;
        // Then iterate thru webpages to get the homepage
        for (CFIndex k=0; k < ABMultiValueGetCount(webpages); k++)
        {
            homepage = (__bridge NSString*)ABMultiValueCopyValueAtIndex(webpages, k);
            NSLog(@"homepage = %@",homepage);

        }
        NSLog(@"URL = %@",homepage); 
        //// get BirthDay
        if((__bridge NSString*)ABRecordCopyValue(person, kABPersonBirthdayProperty)){
            contacts.birthday = (__bridge NSString*)ABRecordCopyValue(person, kABPersonBirthdayProperty);
        }
        if (!contacts.birthday) {
            contacts.birthday = @"";
        }
        //// get Company Name
        if((__bridge NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty)){
            contacts.company = (__bridge NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty);
        }
        if (!contacts.company) {
            contacts.company = @"";
        }
        // get contacts picture, if pic doesn't exists, show standart one

        contacts.img_data = (__bridge NSData *)ABPersonCopyImageData(person);

        if (!contacts.img_data) {
            UIImage *image = [UIImage imageNamed:@"profile@2x.png"];
            NSData *img_data = UIImagePNGRepresentation(image);
            contacts.img_data = img_data;
        }
        //get Phone Numbers
        NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
        ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
        for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);i++) {
            CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
            NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;
            NSString * strippedNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [phoneNumber length])];
            [phoneNumbers addObject:strippedNumber];
            //NSLog(@"All numbers %@", phoneNumbers);
        }
        contacts.arrCallIDs = phoneNumbers;
        /// get Addresss
        NSMutableArray *arry_address = [[NSMutableArray alloc] init];
        ABMutableMultiValueRef multiAddress = ABRecordCopyValue(person, kABPersonAddressProperty);
        for(CFIndex i=0; i<ABMultiValueGetCount(multiAddress);i++){
            CFDictionaryRef address = ABMultiValueCopyValueAtIndex(multiAddress, i);
            NSString *street = (NSString*) CFDictionaryGetValue(address, kABPersonAddressStreetKey);
            NSString *city = (NSString*) CFDictionaryGetValue(address, kABPersonAddressCityKey);
            NSString *state = (NSString*) CFDictionaryGetValue(address, kABPersonAddressStateKey);
            NSString *postal = (NSString*) CFDictionaryGetValue(address, kABPersonAddressZIPKey);
            NSString *country = (NSString*) CFDictionaryGetValue(address, kABPersonAddressCountryKey);
            NSString *country_id = (NSString*) CFDictionaryGetValue(address, kABPersonAddressCountryCodeKey);

            NSMutableDictionary *add_field = [[NSMutableDictionary alloc] init];
            [add_field setValue:street forKey:@"street"];
            [add_field setValue:city forKey:@"city"];
            [add_field setValue:state forKey:@"state"];
            [add_field setValue:postal forKey:@"postal"];
            [add_field setValue:country forKey:@"country"];
            [add_field setValue:country_id forKey:@"country_id"];

           // NSLog(@"Address = %@",add_field);
            [arry_address addObject:add_field];
        }
        contacts.arrAddress = arry_address;
        //get Contact email
        NSMutableArray *contactEmails = [NSMutableArray new];
        ABMultiValueRef multiEmails = ABRecordCopyValue(person, kABPersonEmailProperty);
        for (CFIndex i=0; i<ABMultiValueGetCount(multiEmails); i++) {
            CFStringRef contactEmailRef = ABMultiValueCopyValueAtIndex(multiEmails, i);
            NSString *contactEmail = (__bridge NSString *)contactEmailRef;
            [contactEmails addObject:contactEmail];
            // NSLog(@"All emails are:%@", contactEmails);
        }
        contacts.arrEmails = contactEmails;
        [items addObject:contacts];
        //NSLog(@"Person is: %@", contacts.firstNames);
        //NSLog(@"Phones are: %@", contacts.numbers);
        //NSLog(@"Email is:%@", contacts.emails);
    }
    return items;
} else {
    NSLog(@"Cannot fetch Contacts :( ");
    return NO;
}
}
+(NSArray*)获取所有联系人
{
CFErrorRef*错误=nil;
ABAddressBookRef addressBook=ABAddressBookCreateWithOptions(NULL,NULL);
__block BOOL accessgrated=否;
如果(ABAddressBookRequestAccessWithCompletion!=NULL){//我们在iOS 6上
dispatch\u semaphore\u t sema=dispatch\u semaphore\u create(0);
ABAddressBookRequestAccessWithCompletion(addressBook,^(已授予bool,CFErrorRef错误){
accessgrated=已授予;
调度信号量信号(sema);
});
调度信号量等待(永远调度时间);
}
否则{//我们使用的是iOS 5或更高版本
accessgrated=是;
}
如果(已授予访问权限){
NSLog(@“获取联系信息---->”);
ABAddressBookRef addressBook=ABAddressBookCreateWithOptions(空,错误);
ABRecordRef source=ABAddressBookCopyDefaultSource(地址簿);
CFArrayRef allPeople=abAddressBookCopyArrayFallPeopleInSourceWithSortOrdering(addressBook、source、kABPersonSortByFirstName);
CFIndex nPeople=CFArrayGetCount(所有人);
NSMutableArray*items=[NSMutableArray阵列容量:NPEOPE];
for(int i=0;i对于(CFIndex i=0;i尝试此代码函数,您可以获得您想要的所有信息

+(NSArray *)getAllContacts
{
CFErrorRef *error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        accessGranted = granted;
        dispatch_semaphore_signal(sema);
    });
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

}
else { // we're on iOS 5 or older
    accessGranted = YES;
}
if (accessGranted) {
    NSLog(@"Fetching contact info ----> ");
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
    CFIndex nPeople = CFArrayGetCount(allPeople);
    NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople];
    for (int i = 0; i < nPeople; i++)
    {
        ContactDetailModel *contacts = [[ContactDetailModel alloc] init];  // it is NsObject class. this is contained variables and array which required for get contact information. It is not necessary for you. you can maintain according your requirments. 
        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
        if((__bridge NSString*)ABRecordCopyCompositeName(person)){
            contacts.compse_name =  (__bridge NSString*)ABRecordCopyCompositeName(person);
            NSLog(@"compse_name = %@",contacts.compse_name);
        }
        if (!contacts.compse_name) {
            contacts.compse_name = @"";
        }

        //get First Name and Last Name

        if((__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty)){
            contacts.first_name = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        }
        if((__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty)){
            contacts.last_name =  (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
        }
        if (!contacts.first_name) {
            contacts.first_name = @"";
        }
        if (!contacts.last_name) {
            contacts.last_name = @"";
        }
        NSLog(@"fname and lname = %@ %@",contacts.first_name,contacts.last_name);


        /// URL
        ABMultiValueRef webpages = ABRecordCopyValue(person, kABPersonURLProperty);
        NSString* homepage;
        // Then iterate thru webpages to get the homepage
        for (CFIndex k=0; k < ABMultiValueGetCount(webpages); k++)
        {
            homepage = (__bridge NSString*)ABMultiValueCopyValueAtIndex(webpages, k);
            NSLog(@"homepage = %@",homepage);

        }
        NSLog(@"URL = %@",homepage); 
        //// get BirthDay
        if((__bridge NSString*)ABRecordCopyValue(person, kABPersonBirthdayProperty)){
            contacts.birthday = (__bridge NSString*)ABRecordCopyValue(person, kABPersonBirthdayProperty);
        }
        if (!contacts.birthday) {
            contacts.birthday = @"";
        }
        //// get Company Name
        if((__bridge NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty)){
            contacts.company = (__bridge NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty);
        }
        if (!contacts.company) {
            contacts.company = @"";
        }
        // get contacts picture, if pic doesn't exists, show standart one

        contacts.img_data = (__bridge NSData *)ABPersonCopyImageData(person);

        if (!contacts.img_data) {
            UIImage *image = [UIImage imageNamed:@"profile@2x.png"];
            NSData *img_data = UIImagePNGRepresentation(image);
            contacts.img_data = img_data;
        }
        //get Phone Numbers
        NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
        ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
        for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);i++) {
            CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
            NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;
            NSString * strippedNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [phoneNumber length])];
            [phoneNumbers addObject:strippedNumber];
            //NSLog(@"All numbers %@", phoneNumbers);
        }
        contacts.arrCallIDs = phoneNumbers;
        /// get Addresss
        NSMutableArray *arry_address = [[NSMutableArray alloc] init];
        ABMutableMultiValueRef multiAddress = ABRecordCopyValue(person, kABPersonAddressProperty);
        for(CFIndex i=0; i<ABMultiValueGetCount(multiAddress);i++){
            CFDictionaryRef address = ABMultiValueCopyValueAtIndex(multiAddress, i);
            NSString *street = (NSString*) CFDictionaryGetValue(address, kABPersonAddressStreetKey);
            NSString *city = (NSString*) CFDictionaryGetValue(address, kABPersonAddressCityKey);
            NSString *state = (NSString*) CFDictionaryGetValue(address, kABPersonAddressStateKey);
            NSString *postal = (NSString*) CFDictionaryGetValue(address, kABPersonAddressZIPKey);
            NSString *country = (NSString*) CFDictionaryGetValue(address, kABPersonAddressCountryKey);
            NSString *country_id = (NSString*) CFDictionaryGetValue(address, kABPersonAddressCountryCodeKey);

            NSMutableDictionary *add_field = [[NSMutableDictionary alloc] init];
            [add_field setValue:street forKey:@"street"];
            [add_field setValue:city forKey:@"city"];
            [add_field setValue:state forKey:@"state"];
            [add_field setValue:postal forKey:@"postal"];
            [add_field setValue:country forKey:@"country"];
            [add_field setValue:country_id forKey:@"country_id"];

           // NSLog(@"Address = %@",add_field);
            [arry_address addObject:add_field];
        }
        contacts.arrAddress = arry_address;
        //get Contact email
        NSMutableArray *contactEmails = [NSMutableArray new];
        ABMultiValueRef multiEmails = ABRecordCopyValue(person, kABPersonEmailProperty);
        for (CFIndex i=0; i<ABMultiValueGetCount(multiEmails); i++) {
            CFStringRef contactEmailRef = ABMultiValueCopyValueAtIndex(multiEmails, i);
            NSString *contactEmail = (__bridge NSString *)contactEmailRef;
            [contactEmails addObject:contactEmail];
            // NSLog(@"All emails are:%@", contactEmails);
        }
        contacts.arrEmails = contactEmails;
        [items addObject:contacts];
        //NSLog(@"Person is: %@", contacts.firstNames);
        //NSLog(@"Phones are: %@", contacts.numbers);
        //NSLog(@"Email is:%@", contacts.emails);
    }
    return items;
} else {
    NSLog(@"Cannot fetch Contacts :( ");
    return NO;
}
}
+(NSArray*)获取所有联系人
{
CFErrorRef*错误=nil;
ABAddressBookRef addressBook=ABAddressBookCreateWithOptions(NULL,NULL);
__block BOOL accessgrated=否;
如果(ABAddressBookRequestAccessWithCompletion!=NULL){//我们在iOS 6上
dispatch\u semaphore\u t sema=dispatch\u semaphore\u create(0);
ABAddressBookRequestAccessWithCompletion(addressBook,^(已授予bool,CFErrorRef错误){
accessgrated=已授予;
调度信号量信号(sema);
});
调度信号量等待(永远调度时间);
}
否则{//我们使用的是iOS 5或更高版本
accessgrated=是;
}
如果(已授予访问权限){
NSLog(@“获取联系信息---->”);
ABAddressBookRef addressBook=ABAddressBookCreateWithOptions(空,错误);
ABRecordRef source=ABAddressBookCopyDefaultSource(地址簿);
CFArrayRef allPeople=abAddressBookCopyArrayFallPeopleInSourceWithSortOrdering(addressBook、source、kABPersonSortByFirstName);
CFIndex nPeople=CFArrayGetCount(所有人);
NSMutableArray*items=[NSMutableArray阵列容量:NPEOPE];
for(int i=0;i