Skip to content

Instantly share code, notes, and snippets.

@username0x0a
Last active November 22, 2020 18:13
Show Gist options
  • Save username0x0a/e6310aa6c982dbd276621564b8ab0f57 to your computer and use it in GitHub Desktop.
Save username0x0a/e6310aa6c982dbd276621564b8ab0f57 to your computer and use it in GitHub Desktop.
UIDevice subclass for fetching real carrier display name – may differ from regular -carrierName for virtual operators etc.
@implementation UIDevice (Utils)
- (NSString *)cellularCarrierName
{
return [[[[CTTelephonyNetworkInfo alloc] init] subscriberCellularProvider] carrierName];
}
- (NSString *)cellularCarrierDisplayedName
{
static NSString *displayedName = nil;
static NSDate *lastUpdate = nil;
// Perform an update every hour
if (!lastUpdate || -[lastUpdate timeIntervalSinceNow] > 60*60)
{
UIView *bar = nil;
SEL sel = NSSelectorFromString([ @[ @"status", @"Bar" ] componentsJoinedByString:@""]);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
if ([[UIApplication sharedApplication] respondsToSelector:sel])
bar = [[UIApplication sharedApplication] performSelector:sel];
#pragma clang diagnostic pop
bar = [bar viewForClass:NSClassFromString(
[ @[ @"UI", @"Status", @"BarService", @"ItemView" ] componentsJoinedByString:@""])];
displayedName = [bar valueForKey:@"_serviceString"] ?:
[self cellularCarrierName];
}
return displayedName;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment