1

I'm trying to develop an app, where users can send messages via iMessage. So I'm able to import the contact list from AddressBook, and to send Messages via MessageUI Framework, but I will like to know which of the contacts imported have also iPhone so they can use iMessage capability. Is there any way to find out from the kABPerson...Property?

Here my code:

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
            ABAddressBookRef addressBook = ABAddressBookCreate( );
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {

 CFErrorRef *error = NULL;
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
        CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

        for(int i = 0; i < numberOfPeople; i++) {

            ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );

             NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
             NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));

             //Something like this ?
             BOOL *doeshaveiPhone = (__bridge BOOL *)(ABRecordCopyValue(person, kABPersonPhoneIPhoneLabel));


             NSLog(@"Name:%@ Surname: %@", firstName, lastName);


            ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);

            for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
            {
                CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);
                CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(phones, j);
                NSString *phoneLabel =(__bridge NSString*) ABAddressBookCopyLocalizedLabel(locLabel);

                NSString *phoneNumber = (__bridge NSString *)phoneNumberRef;
                CFRelease(phoneNumberRef);
                CFRelease(locLabel);
                NSLog(@" - %@ (%@)", phoneNumber, phoneLabel);

            }

        }
Mind Pixel
  • 816
  • 7
  • 16
Pau Senabre
  • 4,155
  • 2
  • 27
  • 36

1 Answers1

1

Your code is the best way to determine if people in the Address Book have iMessage. However, there is a disadvantage: it only works if the user properly sets up their contacts. Too often, fields are improperly set which can make developing applications difficult.

This question has been asked before. Here, the top-voted answer says,

your heurisitic might be ok but it relies on the USER correctly setting that attribute.

depending on the context it might be good enough / or not .. dont know.

there is no real way to detect which user really uses ios (no API)

Community
  • 1
  • 1
erdekhayser
  • 6,537
  • 2
  • 37
  • 69