Search code examples
iosobjective-ciphone-xuistatusbar

Detect hotspot crashed in iPhone X


I have sample code as below in viewDidLoad to detect hotspot on / off. It working properly in iPhone 6-8 but crashed in iPhone X.

 UIApplication *app = [UIApplication sharedApplication];
    if(![[app valueForKey:@"statusBar"] valueForKey:@"doubleHeightLabel"])
    {
       //Some code here
    }
    else
    {
        //Some code here
    }

Error Message shown:-

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIStatusBar_Modern 0x7ffed341b7f0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key doubleHeightLabel.'

Any idea?


Solution

  • There's a way to check CNCopyCurrentNetowrkInfo for current network info.

    And there's hacks:

    Obj-c:

    CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
    

    Swift:

    let statusBarHeight = UIApplication.shared.statusBarFrame.size.height
    

    With Personal Hotspot enabled, it returns 40, and returns 20 otherwise.

    Please note that it's not going to work for iPhone X+