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:
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
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!):
Result on iOS 6.1.3 (works without JB and without special signature just build and run)
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.