-3

I know that we can get signal strength by reading the properties from the system status bar view, is there other way that we can get the info?

Miul
  • 1
  • 2
  • 1
    Welcome to SO, Where Developers Learn, Share, & Build Careers! Pure code-writing requests are off-topic on Stack Overflow — we expect questions here to relate to specific programming problems — but we will happily help you write it yourself! Tell us what you've tried, and where you are stuck. This will also help us answer your question better :) – samuellawrentz Aug 06 '18 at 03:32
  • [ios get signal strength swift](https://www.google.com/search?client=safari&rls=en&q=ios+get+signal+strength+swift&ie=UTF-8&oe=UTF-8)? – MadProgrammer Aug 06 '18 at 03:44
  • I found some answers which are reading system status bar info to get the strength. And the paths of the property for iPoneX and other iPhones are not the same. But the way to get the strength is tricky, is there other framework that we can use to get the strength info? – Miul Aug 06 '18 at 03:51
  • Possible duplicate of https://stackoverflow.com/questions/4954389/measuring-cellular-signal-strength – gotnull Aug 06 '18 at 04:23

1 Answers1

-1
        #define IS_IPHONE_X    ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? \
    CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)

    int numberOfBars = 5;           
    int numberOfActiveBars = 0;
    BOOL isFound = NO;

    if (@available(iOS 11.0, *)) {
        numberOfBars = 4;          
    }

    UIApplication *app = [UIApplication sharedApplication];
    id statusBar = [app valueForKey:@"statusBar"];
    if (IS_IPHONE_X) {
        id statusBarView = [statusBar valueForKeyPath:@"statusBar"];
        UIView *foregroundView = [statusBarView valueForKeyPath:@"foregroundView"];
        NSArray *subviews = [[foregroundView subviews][2] subviews];

        for (id subview in subviews) {
            if ([subview isKindOfClass:NSClassFromString(@"_UIStatusBarCellularSignalView")]) {
                numberOfActiveBars = [[subview valueForKey:@"_numberOfActiveBars"] intValue];
                numberOfBars = [[subview valueForKey:@"_numberOfBars"] intValue];
                isFound = YES;
                break;
            }
        }
    } else {
        UIView *foregroundView = [statusBar valueForKey:@"foregroundView"];
        NSArray *subviews = [foregroundView subviews];
        for (id subview in subviews) {
            if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarSignalStrengthItemView") class]]) {
                numberOfActiveBars = [[subview valueForKey:@"_signalStrengthBars"] intValue];
                isFound = YES;
                break;
            }
        }
    }
Miul
  • 1
  • 2
  • 1
    This uses undocumented symbols (anything prefixed with an underscore, `_numberOfActiveBars` and `_numberOfBars` for example) and is likely to get rejected by Apple. Do not use this method if the app needs to be distributed through the App Store. – Sean Kladek Aug 06 '18 at 04:17
  • Thanks for comment, I know that this code is tricky, that is why i ask if there is other way to get the info. Any help is appreciated. – Miul Aug 06 '18 at 04:21
  • As far as I know, no, there isn’t an AppStore safe way to get this info. You can get general reachability info (whether the user has a connection and if it is through LTE or WIFI), but not the actual signal strength. – Sean Kladek Aug 06 '18 at 04:56