在 iOS 中以编程方式获取自己的电话号码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/193182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:19:18  来源:igfitidea点击:

Programmatically get own phone number in iOS

iosiphonephone-number

提问by Vadim

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

有没有办法通过 iPhone SDK 的标准 API 获取自己的电话号码?

采纳答案by Dylan

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:

冒着得到负面评价的风险,我想建议排名最高的解决方案(目前是第一个响应)违反了截至 2009 年 11 月 5 日的最新 SDK 协议。我们的申请只是因为使用它而被拒绝。以下是苹果的回应:

"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.

“出于安全原因,iPhone OS 将应用程序(包括其首选项和数据)限制在文件系统中的唯一位置。此限制是称为应用程序“沙箱”的安全功能的一部分。沙箱是一组精细的-粒度控制限制应用程序对文件、首选项、网络资源、硬件等的访问。”

设备的电话号码在您的应用程序容器中不可用。您需要修改您的应用程序以使其仅在您的目录容器中读取,并将您的二进制文件重新提交到 iTunes Connect,以便您的应用程序重新考虑用于 App Store。

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

这真是令人失望,因为我们想让用户不必输入自己的电话号码。

回答by Andrew Grant

No, there's no legal and reliable way to do this.

不,没有合法且可靠的方法可以做到这一点。

If you find a way, it will be disabled in the future, as it has happened with every method before.

如果您找到了一种方法,它将在将来被禁用,就像以前的每种方法都发生过一样。

回答by Robert Sanders

Update:capability appears to have been removed by Apple on or around iOS 4

更新:Apple 似乎已在 iOS 4 上或周围移除了该功能



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 NOTread from the SIM card.

注意:这将检索在 iPhone 的 iTunes 激活期间输入的“电话号码”,并且可以为空或不正确的值。它不是从 SIM 卡中读取的。

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)

至少在 2.1 中是这样。NSUserDefaults 中还有一些其他有趣的键可能也不会持久。(这是在我使用 UIWebView 的应用程序中)

WebKitJavaScriptCanOpenWindowsAutomatically
NSInterfaceStyle
TVOutStatus
WebKitDeveloperExtrasEnabledPreferenceKey

and so on.

等等。

Not sure what, if anything, the others do.

不知道其他人会做什么,如果有的话。

回答by Igor

Using Private API you can get user phone number on the following way:

使用私有 API,您可以通过以下方式获取用户电话号码:

extern NSString* CTSettingCopyMyPhoneNumber();


+(NSString *) phoneNumber {
NSString *phone = CTSettingCopyMyPhoneNumber();

return phone;
}

Also include CoreTelephony.framework to your project.

还包括 CoreTelephony.framework 到您的项目。

回答by Andres Garcia

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

你可能都知道如果你使用以下代码行,你的应用程序将被 Apple 拒绝

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

here is a reference

这是一个参考

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

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!

希望这可以帮助!

回答by davidgyoung

You cannot use iOS APIs alone to capture the phone number (even in a private app with private APIs), as all known methods of doing this have been patched and blocked as of iOS 11. Even if a new exploit is found, Apple has made clear that they will reject any apps from the app store for using private APIs to do this. See @Dylan's answer for details.

您不能单独使用 iOS API 来捕获电话号码(即使在具有私有 API 的私有应用程序中),因为从 iOS 11 开始,所有已知的执行此操作的方法都已被修补和阻止。即使发现了新的漏洞,Apple 也已做出明确表示他们将拒绝应用商店中使用私有 API 执行此操作的任何应用。有关详细信息,请参阅@Dylan 的回答。

However, there is a legal way to capture the phone number without any user data entry. This is similar to what Snapchat does, but easier, as it does not require the user to type in their own phone number.

但是,有一种合法的方法可以在不输入任何用户数据的情况下捕获电话号码。这与 Snapchat 的做法类似,但更简单,因为它不需要用户输入自己的电话号码。

The idea is to have the app programmatically send a SMS message to a server with the app's unique installation code. The app can then query the same server to see if it has recently received a SMS message from a device with this unique app installation code. If it has, it can read the phone number that sent it. Here's a demo videoshowing the process. As you can see, it works like a charm!

这个想法是让应用程序以编程方式将 SMS 消息发送到具有应用程序唯一安装代码的服务器。然后,该应用程序可以查询同一服务器,以查看它最近是否收到了来自具有此唯一应用程序安装代码的设备的 SMS 消息。如果有,它可以读取发送它的电话号码。这是显示该过程的演示视频。如您所见,它就像一个魅力!

This is not super easy to set up, but it be configured in a few hours at no charge on a free AWS tier with the sample code provided in the tutorial here.

这不是很容易设置,但它可以在几个小时内免费配置在免费的 AWS 层上,使用教程中提供的示例代码在这里

回答by David G?lzh?user

To get you phone number you can read a plist file. It will not work on non-jailbroken iDevices:

要获取电话号码,您可以阅读 plist 文件。它不适用于未越狱的 iDevices:

NSString *commcenter = @"/private/var/wireless/Library/Preferences/com.apple.commcenter.plist";
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:commcenter];
    NSString *PhoneNumber = [dict valueForKey:@"PhoneNumber"];
    NSLog([NSString stringWithFormat:@"Phone number: %@",PhoneNumber]);

I don't know if Apple allow this but it works on iPhones.

我不知道 Apple 是否允许这样做,但它适用于 iPhone。

回答by 0x8BADF00D

No official API to do it. Using private API you can use following method:

没有官方 API 来做到这一点。使用私有 API,您可以使用以下方法:

-(NSString*) getMyNumber {
    NSLog(@"Open CoreTelephony");
    void *lib = dlopen("/Symbols/System/Library/Framework/CoreTelephony.framework/CoreTelephony",RTLD_LAZY);
    NSLog(@"Get CTSettingCopyMyPhoneNumber from CoreTelephony");
    NSString* (*pCTSettingCopyMyPhoneNumber)() = dlsym(lib, "CTSettingCopyMyPhoneNumber");
    NSLog(@"Get CTSettingCopyMyPhoneNumber from CoreTelephony");

    if (pCTSettingCopyMyPhoneNumber == nil) {
        NSLog(@"pCTSettingCopyMyPhoneNumber is nil");
        return nil;
    }
    NSString* ownPhoneNumber = pCTSettingCopyMyPhoneNumber();
    dlclose(lib);
    return ownPhoneNumber;
}

It works on iOS 6 without JB and special signing.

它适用于没有 JB 和特殊签名的 iOS 6。

As mentioned crekeron iOS 7 with JByou need to use entitlements to make it working.

正如在带有 JB 的 iOS 7上提到的creker,您需要使用权利才能使其工作。

How to do it with entitlements you can find here: iOS 7: How to get own number via private API?

如何使用您可以在此处找到的权利: iOS 7:如何通过私有 API 获取自己的号码?

回答by thedazed

AppStore will reject it, as it's reaching outside of application container.

AppStore 将拒绝它,因为它到达应用程序容器之外。

Apps should be self-contained in their bundles, and may not read or write data outside the designated container area

应用程序应在其捆绑包中自包含,并且不得在指定的容器区域之外读取或写入数据

Section 2.5.2 : https://developer.apple.com/app-store/review/guidelines/#software-requirements

第 2.5.2 节:https: //developer.apple.com/app-store/review/guidelines/#software-requirements