0

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
AD Tee
  • 345
  • 4
  • 21

2 Answers2

2

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+

Vlad Z.
  • 3,401
  • 3
  • 32
  • 60
0

It crashes because Apple has changed the object held by key "statusBar". It no longer has an object with key "doubleHeightLabel". As stated in comments, Apple prohibits the usage of private API because of this very issue. As they upgrade iOS version, they will change "statusBar" object as they please, so keys can be missing, or changed, or even to hold a totally different variable.

Solution: Don't use private API. Be a good boi and listen to Apple.

GeneCode
  • 7,545
  • 8
  • 50
  • 85