Measuring cellular signal strength

2019-01-01 07:34发布

问题:

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

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

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.

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

回答1:

I briefly looked at the VAFieldTest project located at Github.

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.

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



回答2:

Get signalStreght 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);


回答3:

It is not very hard.

  1. Link CoreTelephony.framework in your Xcode project
  2. Add the following lines where you need it

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

Apple removed this opportunity.



回答4:

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.

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.



回答5:

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

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

id signalStrength = [[CTTelephonyNetworkInfo new] signalStrength];

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

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



回答6:

Here\'s Lucas\' answer converted to Xamarin, and tested on 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;