14

Old ways don't work any more:

// way 1

void *lib = dlopen("/Symbols/System/Library/Framework/CoreTelephony.framework/CoreTelephony", RTLD_LAZY);
NSString* (*getPhoneNumber)() = dlsym(lib, "CTSettingCopyMyPhoneNumber");

if (getPhoneNumber == nil) {
    NSLog(@"getPhoneNumber is nil");
    return nil;
}
NSString* ownPhoneNumber = getPhoneNumber();

// way 2

extern NSString* CTSettingCopyMyPhoneNumber();
NSString *phone = CTSettingCopyMyPhoneNumber();

Related questions:

Is it possible to detect a phone number of the device in iOS?

[UPDATE]

Provided method works on iOS 6.1.4 without JB and iOS 7.1.2 with JB

Thanks to user creker for hint!

To get number you have to use following way:

1. Code

-(NSString*) getMyNumber {
    NSLog(@"Open CoreTelephony");
    void *lib = dlopen("/Symbols/System/Library/Framework/CoreTelephony.framework/CoreTelephony",RTLD_LAZY);
    NSLog(@"Get CTSettingCopyMyPhoneNumber from CoreTelephony");
    NSString* (*pCTSettingCopyMyPhoneNumber)() = dlsym(lib, "CTSettingCopyMyPhoneNumber");
    NSLog(@"Get CTSettingCopyMyPhoneNumber from CoreTelephony");

    if (pCTSettingCopyMyPhoneNumber == nil) {
        NSLog(@"pCTSettingCopyMyPhoneNumber is nil");
        return nil;
    }
    NSString* ownPhoneNumber = pCTSettingCopyMyPhoneNumber();
    dlclose(lib);
    return ownPhoneNumber;
}

2. Build application without signing: enter image description here

3. Create xml file (entitlements.xml) with following content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>com.apple.coretelephony.Calls.allow</key>
        <true/>
        <key>com.apple.coretelephony.Identity.get</key>
        <true/>
    </dict>
</plist>

Where:

com.apple.coretelephony.Calls.allow — for calls

com.apple.coretelephony.Identity.get — for getting own number

4. Sing app with entitlements.xml Copy files in one folder:

./FolderForRunSinging/
    YourApp.app
    entitlements.xml

entitlements.xml - created xml

YourApp.app - binaries for your app copied from XCode files To find where your binaries you can use command:

$ls -la ~/Library/Developer/Xcode/DerivedData/ | grep YourApp

Path should be like this:

/Users/username/Library/Developer/Xcode/DerivedData/YourApp-cktasembftvbmqaaiiunvljdwocs/Build/Products/Debug-iphoneos/YourApp.app

Run singing command:

$codesign --sign='iPhone Developer: FirstName  SecondName (XXXXXXXX)’
--entitlements entitlements.xml YourApp.app

'iPhone Developer: FirstName SecondName (XXXXXXXX)' - name of your certificate in keychain

enter image description here

5. Copy application YourApp.app on your device using ssh to folder

/Applications

6. Restart springboard (run on device):

$su mobile -c uicache 1>/dev/null 2>&1

Working sample could be found on github

Result on iOS 7.1.2 (JB is required!):

enter image description here

Result on iOS 6.1.3 (works without JB and without special signature just build and run)

enter image description here

Community
  • 1
  • 1
0x8BADF00D
  • 7,138
  • 2
  • 41
  • 34
  • The sample is not working in iOS 8.4. Does it need any additional works to make it work in xcode 6.4 ? – Peer Mohamed Thabib Aug 11 '15 at 10:23
  • 2
    This is a useful answer, but the **answer** should be split out from the question, as a true answer below. Nothing wrong with answering your own question. Even accepting it :) – Nate Sep 18 '16 at 02:00

1 Answers1

9

In order to use this API you need to sign your app with entitlement com.apple.coretelephony.Identity.get with boolean value set to true.

creker
  • 9,400
  • 1
  • 30
  • 47
  • how to sign entitlement.can you please share any link to get mobile number. – Kittu Jun 03 '14 at 12:04
  • 2
    FYI, just in case someone else is interested to use this private API entitlement in an enterprise app, furgeddaboutit. You can create an entitlements plist and add that to the project, but you cannot add com.apple.coretelephony.Identity.get to the provisioning profile (you cannot add custom entitlements to the profile), so the app will build but will tank when trying to run on ios. – Nostradamus Nov 19 '15 at 19:22
  • I understand that we could not add this to the provisioning profile, and so it is not possible to get the user active/current phone number. Thank you Apple, again iOS developers have another challenge against android developers :) – ondermerol Apr 04 '16 at 08:17