0

I want to get own phone number by standard APIs from iPhone SDK? i have used some code snnipts but didn't get phone number.

NSString *num = [[NSUserDefaults standardUserDefaults] stringForKey:@"SBFormattedPhoneNumber]; 
NSLog(@"Phone Number: %@", num);

and

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Preferences/.GlobalPreferences.plist"]; 

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];

NSLog(@"dict =  %@", dict);

and also i want to s app on itunes so didn't want any private method to use.

Thanks for any help

esqew
  • 42,425
  • 27
  • 92
  • 132
I_User
  • 325
  • 3
  • 18
  • 2
    see this http://stackoverflow.com/questions/193182/programmatically-get-own-phone-number-in-ios – MadhuP Jun 13 '13 at 05:03
  • 1
    Odd but true - in the GSM family of protocols, there is no reason for a mobile to know its phone number. There are conventions but they are imperfect. Given that, any solution will be unreliable. – DrC Jun 13 '13 at 05:11
  • 1
    See this link It will be halpfull 'http://stackoverflow.com/questions/193182/programmatically-get-own-phone-number-in-ios' – Prateek Prem Jun 13 '13 at 05:42

1 Answers1

2

There is no reliable way to do this. Apple did this both for security reasons as well as the fact that not all iOS devices have a phone number (think iPad/iPod). However, the only way that seems to work some of the time is to use the device name, search for a user's name, and see if that name exists in the address book, then hope the user's own address book entry exists. But this is not a foolproof method and certainly leads to potential false positive results (especially with common names or poorly maintained address books). Additionally, I have no idea how effective this would be in non-English languages. So use this only as a suggested value to prompt the user (ie. "Is this your phone number?"). Here's some code to help you do this:

// Try to figure out user's name from device name
NSString *deviceName                = UIDevice.currentDevice.name;
NSUInteger index;
if ((index = [deviceName rangeOfString:@"'s iPhone"].location)  != NSNotFound ||
    (index = [deviceName rangeOfString:@"'s iPad"].location)    != NSNotFound ||
    (index = [deviceName rangeOfString:@"'s iPod"].location)    != NSNotFound ||
    (index = [deviceName rangeOfString:@"’s iPhone"].location)  != NSNotFound ||
    (index = [deviceName rangeOfString:@"’s iPad"].location)    != NSNotFound ||
    (index = [deviceName rangeOfString:@"’s iPod"].location)    != NSNotFound)
{
    NSString *potentialName             = [deviceName substringToIndex:index];
    if (potentialName.length > 0)
    {
        // Search against the address book for an entry with the same name
    }
}
Mr. T
  • 12,795
  • 5
  • 39
  • 47
  • Any other unique number which i can use except uuid or mac address.Like we have many information in setting app.(serial number etc.)? any official way .... – I_User Jun 13 '13 at 06:00
  • Well, UUID has been deprecated and replaced with UIDevice.currentDevice.identifierForVendor and ASIdentifierManager.sharedManager.advertisingIdentifier. They come with some caveats like advertisingIdentifier is unchanging and unique for your apps but is something the user can opt out of, and identifierForVendor will change if all of your apps are deleted from that device. Other than that, there isn't much... you can always store (in the app + icloud) a unique token that you generate on the server side, but that's not much better than identiferForVendor. – Mr. T Jun 13 '13 at 06:41
  • Regarding effictiveness on other languages: None. For example, the German default is "iPhone von $NAME". – patric.schenke Jun 13 '13 at 08:30
  • @patric.schenke haha, good to know. yea... this is decently effective but only some of the time. :) But it would be pretty cool if someone went through all the different languages and compiled a comprehensive list of all the possible device name templates. – Mr. T Jun 13 '13 at 08:55
  • There is a framework out there which does it (and some other tricks). Google for "ios get me contact" or something like that. However, the risk of being rejected by doing such things seems to be high. – patric.schenke Jun 13 '13 at 12:52