0

我想通过 iPhone SDK 的标准 API 获取自己的电话号码?我使用了一些代码片段,但没有得到电话号码。

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

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

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];

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

而且我想在 iTunes 上安装应用程序,所以不想使用任何私有方法。

谢谢你的帮助

4

1 回答 1

2

没有可靠的方法来做到这一点。Apple 这样做既是出于安全原因,也是因为并非所有 iOS 设备都有电话号码(想想 iPad/iPod)。但是,有时似乎可行的唯一方法是使用设备名称,搜索用户的名称,然后查看该名称是否存在于通讯簿中,然后希望用户自己的通讯簿条目存在。但这不是一种万无一失的方法,并且肯定会导致潜在的误报结果(尤其是常见的名称或维护不善的地址簿)。此外,我不知道这在非英语语言中会有多有效。因此,仅将其用作提示用户的建议值(即“这是您的电话号码吗?”)。这里有一些代码可以帮助你做到这一点:

// 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
    }
}
于 2013-06-13T05:45:24.200 回答