ios 测量蜂窝信号强度

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

Measuring cellular signal strength

iosiphoneobjective-ccocoa-touch

提问by bhatti

I am developing a non-appstore app for iOS. I want to read the cellular signal strength in my code.

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

I know Apple doesn't provide any API by which we can achieve this.

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

Is there any private API that can be used to achieve this? I have gone through the various threads regarding this issue but could not find any relevant info.

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

It is completely possible because there is an app in the app-storefor detecting the carrier's signal strength.

完全有可能,因为应用商店里有一个检测运营商信号强度的应用。

采纳答案by Niels Castle

I briefly looked at the VAFieldTest project located at Github.

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

There seems to be getSignalStrength()and register_notification()functions in Classes/VAFieldTestViewController.m that might be interesting to you as they call into CoreTelephony.framework.

Classes/VAFieldTestViewController.m 中似乎有getSignalStrength()register_notification()函数,当它们调用CoreTelephony.framework.

I am pretty confident that some of the used calls are undocumented in the CoreTelephony framework documentation from Appleand therefore private - any app in the AppStore must have slipped passed inspection.

我非常有信心,AppleCoreTelephony 框架文档中没有记录一些使用过的调用,因此是私有的——AppStore 中的任何应用程序都必须通过检查。

回答by vualoaithu

Get signalStreght IOS9:

获取信号强度 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);

回答by Michael Dorner

It is not very hard.

这不是很难。

  1. Link CoreTelephony.framework in your Xcode project
  2. Add the following lines where you need it
  1. 在 Xcode 项目中链接 CoreTelephony.framework
  2. 在需要的地方添加以下几行

Code:

代码:

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

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

And you are done.

你已经完成了。

Update May 2016

2016 年 5 月更新

Apple removed this opportunity.

苹果取消了这个机会。

回答by Lucas Freitas de Oliveira

To get signal streght in iOS 9 or above in Swift 3, without using the private API from CoreTelephony - CTGetSignalStrength(). Just scouring the statusBar view.

要在 Swift 3 中的 iOS 9 或更高版本中获得信号强度,而不使用来自 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

}

Attention: If the status bar is hidden, the key "statusBar" will return nil.

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

回答by Alessandro Vendruscolo

I haven't tested it, but apparently this is now a method of CTTelephonyNetworkInfoinstead of a global/static function.

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

The return type is id, so I think you get either a NSDictionary(as the _cachedSignalStrengthivar implies) or an NSNumber(as the old function implies).

返回类型是id,所以我认为您会得到 a NSDictionary(如_cachedSignalStrengthivar 所暗示的)或 an NSNumber(如旧函数所暗示的那样)。

id signalStrength = [[CTTelephonyNetworkInfo new] signalStrength];

This changed in iOS 8.3, as you can see from the commit.

这在 iOS 8.3 中发生了变化,您可以从commit 中看到。

Note that this is still not documented! So if your app will go in the App Store, take your precautions.

请注意,这仍然没有记录!因此,如果您的应用程序将进入 App Store,请采取预防措施。

回答by Eliot Gillum

Here's Lucas' answer converted to Xamarin, and tested on iOS 10.2.1:

这是 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;