-1

I've been researching how to find the owner phone number of a iOS device, but with no luck. There are post all around showing thirdparty frameworks that allow to do that, but are rejected from Apple.

In my app I'm accessing to the AddressBook Contact and getting all phone contacts, with Name and Phone. In my case (with my iPhone, I didn't test in other iPhones) I can see my own number listed with the rest of contacts. Is there a way to find out/flag or recognize a contact as owner of the iPhone?

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));


             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);

            }

        }
Kara
  • 6,115
  • 16
  • 50
  • 57
Pau Senabre
  • 4,155
  • 2
  • 27
  • 36
  • 2
    Apple highly protects the privacy of the device's owner, especially from the developers. why do you need to fetch such a private information _without_ the owner's permission? that is always suspicious... – holex Apr 25 '14 at 10:53
  • @holex I want the owner's permission actually. I would like to ask the owner, as I'm doing to access the contact list. Is there any way? – Pau Senabre Apr 25 '14 at 10:57
  • 2
    @PauSenabre, if you want to get the owner's permission, then ask the phone number form them directly in your app via an input field... don't you think that would be the proper solution? – holex Apr 25 '14 at 11:00
  • @PauSenabre check this link for get full information. http://stackoverflow.com/questions/193182/programmatically-get-own-phone-number-in-ios – Darshan Kunjadiya Apr 25 '14 at 11:00
  • I see, I guess that's it. I'll have to ask the user in text field. Thanks for the help anyway. – Pau Senabre Apr 25 '14 at 11:03
  • 2
    @DarshanKunjadiya It's not necessary to add a comment instructing the OP to look at your answer. It's also cheesy to ask for an up vote on your answer. – Abizern Apr 25 '14 at 11:05
  • @Abizern He is giving away free flag points for me though :) – Desdenova Apr 25 '14 at 11:06
  • @DarshanKunjadiya Also - rather than linking to a related SO question and answer, just flag the question as a duplicate. Particularly if you are just going to duplicate the answer from there over here. – Abizern Apr 25 '14 at 11:08
  • @Abizern sorry about that and thanks Desdenova for support. – Darshan Kunjadiya Apr 25 '14 at 11:08

2 Answers2

0

The phone number is not accessible from any Apple public API which means you can't get it.

If you are implementing a dialer application I can't see what you would nee the own phone number for. It's not like you are going to call yourself?

OR

You can get the users phone number from the NSUserDefault:

That is for Jailbroken devices. You should not use these codes if you want your app to be uploaded to AppStore.

[[NSUserDefaults standardUserDefaults] objectForKey:@"SBFormattedPhoneNumber"]

But this will only work if the user has entered his phone number. There is no way in the official SDK to read anything from the SIM card.

Darshan Kunjadiya
  • 3,323
  • 1
  • 29
  • 31
-1

The best thing you can do is already done in this library, give it a try: ABGetMe

You can toggle to use private API-s or nit with ABGETME_ENABLE_PRIVATE_APIS preprocessor macro.

Laszlo
  • 2,803
  • 2
  • 28
  • 33