xcode 准确读取iPhone信号强度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14604273/
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-09-15 02:40:05  来源:igfitidea点击:

Accurately reading of iPhone signal strength

iphoneobjective-cxcodesignals

提问by stevepkr84

There are a few questions on this already, but nothing in them seems to provide accurate results. I need to determine simply if the phone is connected to a cell network at a given moment.

已经有一些关于此的问题,但其中似乎没有提供准确的结果。我需要简单地确定手机是否在给定时刻连接到蜂窝网络。

http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Reference/CTCarrier/Reference/Reference.html

http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Reference/CTCarrier/Reference/Reference.html

This class seems to be documented incorrectly, returning values for mobileCountryCode, isoCountryCode and mobileNetworkCode where no SIM is installed to the phone. carrierName indicates a 'home' network or a previous home network if the phone has been unlocked.

此类似乎记录不正确,返回了手机未安装 SIM 卡的 mobileCountryCode、isoCountryCode 和 mobileNetworkCode 的值。如果手机已解锁,则carrierName 表示“家庭”网络或以前的家庭网络。

I also looked up and found some people claiming the following to work, which uses an undocumented method of the CoreTelephony framework, but the results have been useless to me, reporting seemingly random figures, where perhaps it is not itself updating consistently.

我还查了一下,发现有些人声称以下方法有效,它们使用了 CoreTelephony 框架的未记录方法,但结果对我来说毫无用处,报告了看似随机的数字,也许它本身并没有持续更新。

-(int) getSignalStrength
{
    void *libHandle = dlopen("/System/Library/Frameworks/CoreTelephony.framework/CoreTelephony", RTLD_LAZY);
    int (*CTGetSignalStrength)();
    CTGetSignalStrength = dlsym(libHandle, "CTGetSignalStrength");
    if( CTGetSignalStrength == NULL) NSLog(@"Could not find CTGetSignalStrength");
    int result CTGetSignalStrength();
    dlclose(libHandle);
    return result;
}

Thanks.

谢谢。

Edit: The app is connected to an internal wifi and must remain so, making a reachability check more difficult.

编辑:该应用程序已连接到内部 wifi,并且必须保持连接状态,这使得可达性检查变得更加困难。

采纳答案by stevepkr84

Ok I think I have the correct solution now, which was a bit simpler in the end.

好的,我想我现在有了正确的解决方案,最终更简单一些。

The issue with the CTGetSignalStrength() method is that it works normally, but if you remove a sim, it reports the last signal before the removal. I found another method in the same framework called CTSIMSupportGetSIMStatus(), also undocumented, which can tell you if a SIM is currently connected. Using both as follows should confirm the current network signal.

CTGetSignalStrength() 方法的问题在于它可以正常工作,但是如果您删除一个 sim,它会报告删除前的最后一个信号。我在同一个框架中找到了另一种方法,称为 CTSIMSupportGetSIMStatus(),也没有记录,它可以告诉您当前是否连接了 SIM 卡。如下使用两者应确认当前网络信号。

First declare the methods:

首先声明方法:

NSString * CTSIMSupportGetSIMStatus();
int CTGetSignalStrength();

Then check connectivity to cell network like so:

然后检查与蜂窝网络的连接,如下所示:

NSString *status = CTSIMSupportGetSIMStatus();
int signalstrength = CTGetSignalStrength();
BOOL connected = ( [status isEqualToString: @"kCTSIMSupportSIMStatusReady"] && signalstrength > 0 );

回答by wjl

I'm playing with this function and I've noticed you're calling it in an interesting way. I'm calling it by adding CoreTelephony.framework as a compile-time link. For the function itself, you'll want to declare it's prototype somewhere (perhaps immediately above the method you call from):

我正在玩这个函数,我注意到你以一种有趣的方式调用它。我通过添加 CoreTelephony.framework 作为编译时链接来调用它。对于函数本身,您需要在某处声明它的原型(可能就在您调用的方法上方):

int CTGetSignalStrength();

int CTGetSignalStrength();

This needs to be declared since it isn't in a public header for CoreTelephony.

这需要声明,因为它不在 CoreTelephony 的公共标头中。

Now, I built a simple app that prints signal strength every second.

现在,我构建了一个简单的应用程序,每秒打印一次信号强度。

int CTGetSignalStrength();

- (void)viewDidLoad
{
    [super viewDidLoad];

    while (true) {
        printf("signal strength: %d\n", CTGetSignalStrength());
        sleep(1);
    }
}

I ran it on my iPad mini and it shows steady values until I picked it up, where the number went up. Wrapping my iPad in tin foil (tin foil is a debugging tool I have never used before) caused the number to go down. When I put my iPad in airplane mode, it kept repeating the last value, so this will not be an accurate measure for you.

我在我的 iPad mini 上运行它,它显示稳定的值,直到我拿起它,数字上升。用锡纸包裹我的 iPad(锡纸是我以前从未使用过的调试工具)导致数字下降。当我将 iPad 置于飞行模式时,它不断重复上一个值,因此这对您来说不是一个准确的度量。

If you want to test if a device currently has a cellular data network connection, you may be more interested in Reachability, specifically kSCNetworkReachabilityFlagsIsWWAN.

如果您想测试设备当前是否具有蜂窝数据网络连接,您可能对Reachability更感兴趣,特别是kSCNetworkReachabilityFlagsIsWWAN