【问题标题】:Is there any way to get own phone number by standard APIs from iPhone SDK?有没有办法通过 iPhone SDK 的标准 API 获取自己的电话号码?
【发布时间】:2013-06-13 04:56:39
【问题描述】:

我想通过 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 上安装应用程序,所以不想使用任何私有方法。

感谢您的帮助

【问题讨论】:

标签: ios objective-c


【解决方案1】:

没有可靠的方法来做到这一点。 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
    }
}

【讨论】:

  • 除 uuid 或 mac 地址之外我可以使用的任何其他唯一编号。就像我们在设置应用程序时有很多信息一样。(序列号等)?任何官方方式....
  • 好吧,UUID 已被弃用,取而代之的是 UIDevice.currentDevice.identifierForVendor 和 ASIdentifierManager.sharedManager.advertisingIdentifier。它们带有一些警告,例如广告标识符对于您的应用程序来说是不变的和唯一的,但用户可以选择退出,如果从该设备中删除您的所有应用程序,identifierForVendor 将发生变化。除此之外,没有太多...您始终可以存储(在应用程序 + icloud 中)您在服务器端生成的唯一令牌,但这并不比 identiferForVendor 好多少。
  • 关于其他语言的有效性:无。例如,德语默认为“iPhone von $NAME”。
  • @patric.schenke 哈哈,很高兴知道。是的...这是相当有效的,但只是在某些时候。 :) 但是,如果有人浏览了所有不同的语言并编制了所有可能的设备名称模板的综合列表,那将是非常酷的。
  • 有一个框架可以做到这一点(以及其他一些技巧)。谷歌“ios让我联系”或类似的东西。但是,这样做被拒绝的风险似乎很高。
猜你喜欢
  • 1970-01-01
  • 2013-10-30
  • 2020-03-25
  • 2013-03-22
  • 2022-07-10
  • 2010-10-10
  • 2011-07-19
  • 1970-01-01
相关资源
最近更新 更多