【问题标题】:Measuring cellular signal strength测量蜂窝信号强度
【发布时间】:2011-06-24 16:09:52
【问题描述】:

我正在为 iOS 开发一个非 appstore 应用程序。我想在我的代码中读取蜂窝信号强度。

我知道 Apple 没有提供任何 API 来实现这一点。

是否有任何私有 API 可用于实现此目的?我已经浏览了有关此问题的各种线程,但找不到任何相关信息。

完全可以,因为有app in the app-store可以检测运营商的信号强度。

【问题讨论】:

  • 来自 VAFieldTest 的值是否准确?

标签: ios iphone objective-c cocoa-touch


【解决方案1】:

获取信号强度IOS9:

UIApplication *app = [UIApplication sharedApplication];  
NSArray *subviews = [[[app valueForKey:@"statusBar"]     valueForKey:@"foregroundView"] subviews];  
NSString *dataNetworkItemView = nil;  
     for (id subview in subviews) {  
   if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarSignalStrengthItemView") class]])  
   {  
        dataNetworkItemView = subview;  
        break;  
    }  
 }  
int signalStrength = [[dataNetworkItemView valueForKey:@"signalStrengthRaw"] intValue];  
NSLog(@"signal %d", signalStrength);

【讨论】:

  • 适用于 iOS9 设备(不适用于模拟器)
  • 这是一个私有 API github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/… UIKit 被列为公共,但它似乎是一个私有值。
  • 跟进。我为此构建了一个小演示,在 iTunes Connect 中创建了一个应用程序,并验证了商店提交应用程序存档。这不是万无一失的,但表面上会触发私有 API 错误?
  • 不幸的是,iOS12 设备崩溃了
【解决方案2】:

这并不难。

  1. 在您的 Xcode 项目中链接 CoreTelephony.framework
  2. 在需要的地方添加以下行

代码:

int CTGetSignalStrength(); // private method (not in the header) of Core Telephony

- (void)aScanMethod
{
    NSLog(@"%d", CTGetSignalStrength()); // or do what you want
}

你就完成了。

2016 年 5 月更新

Apple 取消了这个机会。

【讨论】:

  • 它在 iOS 9.1 上运行,但无论我使用 LTE 还是 Wi-Fi,都会返回值 0。
【解决方案3】:

我简要查看了位于 Github 的 VAFieldTest 项目。

Classes/VAFieldTestViewController.m 中似乎有 getSignalStrength()register_notification() 函数,当它们调用 CoreTelephony.framework 时,您可能会感兴趣。

我非常有信心,一些使用过的调用在CoreTelephony framework documentation from Apple 中没有记录,因此是私有的——AppStore 中的任何应用程序都必须通过检查。

【讨论】:

  • @Niels Castle 如何将使用 VAFieldTest 应用程序获得的信号强度转换为 dBm?
  • 点击 Github 链接,您将能够直接与 VAFieldTest 的维护者 Vlad 互动。
  • 是否也可以获取其他参数:RSCP(信号电平)、SC(扰码)和 EcNo(信噪比)?
  • 该项目在 iOS 9 上几乎立即崩溃。
  • 知道 OpenSignal 是如何测量信号强度的吗?他们在主屏幕上显示技术和信号强度(在 iOS 10 上测试),数值非常准确。
【解决方案4】:

在 iOS 9 或更高版本的 Swift 3 中获取信号强度,而不使用来自 CoreTelephony 的私有 API - CTGetSignalStrength()。只是搜索 statusBar 视图。

func getSignalStrength() -> Int {

    let application = UIApplication.shared
    let statusBarView = application.value(forKey: "statusBar") as! UIView
    let foregroundView = statusBarView.value(forKey: "foregroundView") as! UIView
    let foregroundViewSubviews = foregroundView.subviews

    var dataNetworkItemView:UIView!

    for subview in foregroundViewSubviews {
        if subview.isKind(of: NSClassFromString("UIStatusBarSignalStrengthItemView")!) {
            dataNetworkItemView = subview
            break
        } else {
            return 0 //NO SERVICE
        }
    }

    return dataNetworkItemView.value(forKey: "signalStrengthBars") as! Int

}

注意:如果状态栏被隐藏,“statusBar”键会返回nil。

【讨论】:

  • 它伤害了我的灵魂,但它符合要求:它可以工作并且不使用任何私有 API。谢谢!
  • @EliotGillum 苹果同意吗?
  • 使用的密钥在 ios 10 中不存在
  • 它使用私有 API : "value(forKey: "signalStrengthBars")"
  • @user100 是的,我已经在 App Store 中成功提交了 2 个应用程序。
【解决方案5】:

我还没有测试过,但显然这现在是CTTelephonyNetworkInfo 的方法,而不是全局/静态函数。

返回类型是id,所以我认为你会得到NSDictionary(正如_cachedSignalStrength ivar 所暗示的那样)或NSNumber(如旧函数所暗示的那样)。

id signalStrength = [[CTTelephonyNetworkInfo new] signalStrength];

这在 iOS 8.3 中有所改变,您可以从 commit 中看到。

请注意,这仍然没有记录!因此,如果您的应用会在 App Store 上架,请采取预防措施。

【讨论】:

  • signalStrength 在 iOS 9 中返回 null
  • 我正在尝试获取 iOS 9 的蜂窝信号强度,但如上所述它返回 0。使用 UIStatusBarSignalStrengthItemView 在后台模式下不起作用,因此它没有用,因为我需要获取信号强度背景。请让我知道是否有人能够获得这些值。
【解决方案6】:

这是 Lucas 转换为 Xamarin 并在 iOS 10.2.1 上测试的答案:

var application = UIApplication.SharedApplication;
var statusBarView = application.ValueForKey(new NSString("statusBar")) as UIView;
var foregroundView = statusBarView.ValueForKey(new NSString("foregroundView")) as UIView;

UIView dataNetworkItemView = null;
foreach (UIView subview in foregroundView.Subviews)
{
    if ("UIStatusBarSignalStrengthItemView" == subview.Class.Name)
    {
        dataNetworkItemView = subview;
        break;
    }
}
if (null == dataNetworkItemView)
    return false; //NO SERVICE

int bars = ((NSNumber)dataNetworkItemView.ValueForKey(new NSString("signalStrengthBars"))).Int32Value;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    相关资源
    最近更新 更多