views:

17338

answers:

7

Is there any way to get own phone number by standard APIs from iPhone SDK?

+8  A: 

No, there's no way to do this.

Andrew Grant
Not sure why this is getting so many down-votes -- it's as correct as it gets.
Nathan de Vries
+10  A: 

Other apps that I have seen bring up the contact picker and ask the user to choose themselves.

Chase Seibert
+40  A: 

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.

Robert Sanders
Ace! Thanks Robert )
Stream
There's been some recent press about the security concerns behind this ability: http://www.theregister.co.uk/2009/09/30/iphone_security/
JeffH
Any updates on the press and Apple's resolution?
Bryan
Apple just rejected my app for using this. http://img.ly/dbZ
Mugunth Kumar
Don't do it. Ask the user for the number via keypad or contact chooser. You should make any information gathering as explicit as possible to the user. It may not be the most convenient, but it's very easy to save the number, so you only have to ask for it once.
David Kanarek
+1  A: 

I don't think it would be 100% reliable anyway. I had my old number transfered to the iPhone's SIM by the cell operator and the iPhone still doesn't show it in the 'My Number' header at the top of the contacts section. (This is after I did a full firmware wipe for other reasons)

frou
Yes, definitely this feature should be used only as an addition to the contact picker. Sometimes, it also can be useful to scan an AddressBook for the phone number with a suffix retrieved from the Defaults by SBFormattedPhoneNumber key.
Stream
+1  A: 

Here you can find how to get the iphone sim number as string

http://blog.timeister.com/2009/06/25/objective-c-get-iphone-number/

Adrian

+35  A: 

At the risk of getting negative marks, I want to suggest that the highest ranking solution (currently the first response) violates the latest SDK Agreement as of Nov 5, 2009. Our application was just rejected for using it. Here's the response from Apple:

"For security reasons, iPhone OS restricts an application (including its preferences and data) to a unique location in the file system. This restriction is part of the security feature known as the application's "sandbox." The sandbox is a set of fine-grained controls limiting an application's access to files, preferences, network resources, hardware, and so on."

The device's phone number is not available within your application's container. You will need to revise your application to read only within your directory container and resubmit your binary to iTunes Connect in order for your application to be reconsidered for the App Store.

This was a real disappointment since we wanted to spare the user having to enter their own phone number.

Dylan
+3  A: 

As you probably all ready know if you use the following line of code, your app will be rejected by Apple

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

here is a reference

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

you can use the following information instead

NSString *phoneName = [[UIDevice currentDevice] name];

NSString *phoneUniqueIdentifier = [[UIDevice currentDevice] uniqueIdentifier];

and so on

@property(nonatomic,readonly,retain) NSString    *name;              // e.g. "My iPhone"
@property(nonatomic,readonly,retain) NSString    *model;             // e.g. @"iPhone", @"iPod Touch"
@property(nonatomic,readonly,retain) NSString    *localizedModel;    // localized version of model
@property(nonatomic,readonly,retain) NSString    *systemName;        // e.g. @"iPhone OS"
@property(nonatomic,readonly,retain) NSString    *systemVersion;     // e.g. @"2.0"
@property(nonatomic,readonly) UIDeviceOrientation orientation;       // return current device orientation
@property(nonatomic,readonly,retain) NSString    *uniqueIdentifier;  // a string unique to each device based on various hardware info.

Hope this helps!

Andres Garcia