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
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.
此类似乎记录不正确,返回mobileCountryCode,isoCountryCode和mobileNetworkCode的值,其中没有为手机安装SIM卡。 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,必须保持这样,使可达性检查更加困难。
2 个解决方案
#1
9
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
.
如果要测试设备当前是否具有蜂窝数据网络连接,您可能对可达性更感兴趣,特别是kSCNetworkReachabilityFlagsIsWWAN。
#2
5
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 );
#1
9
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
.
如果要测试设备当前是否具有蜂窝数据网络连接,您可能对可达性更感兴趣,特别是kSCNetworkReachabilityFlagsIsWWAN。
#2
5
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 );