I accessing contacts and sending it to server, my question is do I have any possibility of detecting which country contact belongs to? Thank you.
答案 0 :(得分:2)
请按照以下步骤操作。
1:从设备中获取电话号码。
2:您需要从电话号码中找到国家/地区代码。电话号码可以以不同的格式保存在设备中。国际格式,本地格式,国家/地区代码,没有国家/地区代码等。因此,您需要考虑所有这些情况,以便从电话号码中拆分国家/地区代码。以下代码可帮助您从电话号码中获取国家/地区代码。
- (NSString *)getDiallingCodeFromPhoneNumber:(NSString *)phoneNumber
{
NSString *countryCode = @"not found";
NSString *code;
NSString *dialledNumber = [[phoneNumber componentsSeparatedByCharactersInSet:[NSCharacterSet
characterSetWithCharactersInString:@"-() "]]
componentsJoinedByString:@""];
NSString *interNationalCode = [dialledNumber substringToIndex:2];
NSArray *allCodes = [self.diallingCodesDictionary allValues];
int idx;
if([dialledNumber hasPrefix:@"+"])
{
dialledNumber = [dialledNumber substringFromIndex:1];
}
else if([interNationalCode isEqualToString:@"00"])
{
dialledNumber = [dialledNumber substringFromIndex:2];
}
for (idx = 1 ; idx < dialledNumber.length; idx++) {
code = [dialledNumber substringToIndex:idx];
if([allCodes containsObject:code])
{
countryCode = code;
break;
}
}
return countryCode;
}
3:您将从互联网上获取国家/地区代码和国家/地区名称列表。将其添加为捆绑中的plist。 (我从第三部分HMDiallingCode
)
4:现在,您将从该plist获取与您的国家/地区代码相对应的国家/地区名称。
5:如果找不到countryCode =,则表示电话号码未与国家/地区代码一起保存。这清楚地表明,该特定电话号码是本地号码。因此,在这种情况下,您可以从NSLocale
。
答案 1 :(得分:1)
答案 2 :(得分:1)
尝试使用此代码功能,您可以获得所有信息。
+(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;
}
}