1

Currently I'm using the following code to get the local device phone number :

NSString *num = [[NSUserDefaults standardUserDefaults]
                 stringForKey:@”SBFormattedPhoneNumber”];

NSLog(@”Phone Number: %@”, num);

but it can't able to get the number, it shows null. Can any one guide me how to solve this problem!!

Thanks in advance

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
siva
  • 1,402
  • 2
  • 14
  • 28

4 Answers4

2

There is no supported way to get the device's phone number. Any method (such as "SBFormattedPhoneNumber" mentioned in the other answers) that happens to work for now is liable to get your app rejected and is liable to stop working at any point without notice.

If you need the phone number, just ask the user to enter it.

Anomie
  • 92,546
  • 13
  • 126
  • 145
1

Use below code

To Store a phone number string

[[NSUserDefaults standardUserDefaults] setObject:@"1234567890")  
                                        forKey:@"SBFormattedPhoneNumber"];

To get the phone string back.

   NSString* num = [[NSUserDefaults standardUserDefaults] objectForKey:@"SBFormattedPhoneNumber"];

Read the blog tutorial.

http://ayeapi.blogspot.com/2009/12/sbformatphonenumber-is-lie.html

Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
0

Here is the solution !!! Check this link or read answer below from same link !!!

Answer from LINK:

Just to expand on an earlier answer, something like this does it for me:

NSString *num = [[NSUserDefaults standardUserDefaults] stringForKey:@"SBFormattedPhoneNumber"];

Note: This retrieves the "Phone number" that was entered during the iPhone's iTunes activation and can be null or an incorrect value. It's NOT read from the SIM card.

At least that does in 2.1. There are a couple of other interesting keys in NSUserDefaults that may also not last. (This is in my app which uses a UIWebView)

WebKitJavaScriptCanOpenWindowsAutomatically
NSInterfaceStyle
TVOutStatus
WebKitDeveloperExtrasEnabledPreferenceKey

and so on.

Not sure what, if anything, the others do.

Community
  • 1
  • 1
Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147
  • please don't just copy other answers. **if the answer is on stack overflow**, a simple link will suffice. thanks. – Nate Jul 26 '12 at 01:56
0

Saving Data to NSUserDefaults.

[[NSUserDefaults standardUserDefaults] setObject:@"1234567890")  
                                        forKey:@"SBFormattedPhoneNumber"];

 NSString* num = [[NSUserDefaults standardUserDefaults] objectForKey:@"SBFormattedPhoneNumber"];

It may help you..

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Nirav Jain
  • 5,088
  • 5
  • 40
  • 61