通过我的应用程序发送SMS时,它应该仅将其发送到移动电话,但是对于某些联系人,当两个号码同时存在时,一个号码是固定电话,另一个号码是移动电话,它也会发送到固定电话。

- (NSMutableArray*)getContactsWithAddressBook:(ABAddressBookRef )addressBook {

contactList = [[NSMutableArray alloc] init];
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for (int i=0;i < nPeople;i++) {
    NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];
    ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);

    //For username and surname
    ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));
    CFStringRef firstName, lastName;
    firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
    lastName  = ABRecordCopyValue(ref, kABPersonLastNameProperty);
    [dOfPerson setObject:[NSString stringWithFormat:@"%@ %@", firstName, lastName] forKey:@"name"];

    //For Email ids
    ABMutableMultiValueRef eMail  = ABRecordCopyValue(ref, kABPersonEmailProperty);
    if(ABMultiValueGetCount(eMail) > 0) {
        [dOfPerson setObject:(__bridge NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:@"email"];
    }

    NSLog(@"ABMultiValueGetCount(phones)=%ld",ABMultiValueGetCount(phones));
    //For Phone number
    NSString* mobileLabel;
    for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
        mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
        if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
        {
            [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];

        }
        else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
        {
            [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];
            break ;
        }
    }
    [contactList addObject:dOfPerson];
}
return contactList;

}

最佳答案

Google的iOS的电话号码处理库libPhoneNumber有一个漂亮的端口。

它可以帮助您区分固定电话,移动电话,免费电话,高级电话号码等等。您可以使用Cocoapods将此库添加到您的项目中,并按照README的说明进行操作。

这是链接(iOS端口):https://github.com/iziz/libPhoneNumber-iOS
Google libphonenumber库(对于Android):https://github.com/googlei18n/libphonenumber

它们两者都具有几乎相同的功能,并且在检测数字是否有效时非常准确。每当我们遇到无法接收SMS的电话号码(例如座机)时,它为我们节省了很多时间和金钱。

但是,正如评论中提到的那样,大多数固定电话或固定电话号码都可以接收SMS,在这种情况下,您应该询问用户是否要使用指定号码的SMS。

谢谢你
芬内克
祝您编码愉快!

关于ios - 如何防止我的应用向固定电话发送短信,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40841011/

10-13 08:50