Skip to content

Instantly share code, notes, and snippets.

@mkuliszkiewicz
Last active September 4, 2018 20:11
Show Gist options
  • Save mkuliszkiewicz/68d82989a8fc1f8ac9b764a24b27fbf0 to your computer and use it in GitHub Desktop.
Save mkuliszkiewicz/68d82989a8fc1f8ac9b764a24b27fbf0 to your computer and use it in GitHub Desktop.
CTTelephonyNetworkInfo
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
@class CTCarrier;
@interface ANCarrierMeta: NSObject
@property (nonatomic, copy, readonly) NSString *name;
@property (nonatomic, copy, readonly) NSString *countryCode;
@property (nonatomic, copy, readonly) NSString *networkCode;
- (instancetype)initWith:(NSString *)name
countryCode:(NSString *)countryCode
networkCode:(NSString *)networkCode;
+ (instancetype)makeWithCarrier:(CTCarrier *)carrier;;
@end
@interface ANCarrierObserver: NSObject
@property (nonatomic, strong, nullable, readonly) ANCarrierMeta *carrierMeta;
+ (instancetype)shared;
@end
@interface ANCarrierObserver()
@property (nonatomic, strong, nullable, readwrite) ANCarrierMeta *carrierMeta;
@property (nonatomic, strong) CTTelephonyNetworkInfo *networkInfo;
@end
@interface ANCarrierMeta()
@property (nonatomic, copy, readwrite) NSString *name;
@property (nonatomic, copy, readwrite) NSString *countryCode;
@property (nonatomic, copy, readwrite) NSString *networkCode;
@end
@implementation ANCarrierMeta
- (instancetype)initWith:(NSString *)name
countryCode:(NSString *)countryCode
networkCode:(NSString *)networkCode;
{
if (self = [super init]) {
self.name = name;
self.countryCode = countryCode;
self.networkCode = networkCode;
}
return self;
}
+ (instancetype)makeWithCarrier:(CTCarrier *)carrier {
return [[ANCarrierMeta alloc] initWith:carrier.carrierName
countryCode:carrier.mobileCountryCode
networkCode:carrier.mobileNetworkCode];
}
@end
@implementation ANCarrierObserver
+ (instancetype)shared {
static ANCarrierObserver *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[ANCarrierObserver alloc] init];
// Do any other initialisation stuff here
});
return sharedInstance;
}
- (instancetype)init
{
self = [super init];
if (self) {
self.networkInfo = [CTTelephonyNetworkInfo new];
__weak typeof(self) weakSelf = self;
self.networkInfo.subscriberCellularProviderDidUpdateNotifier = ^(CTCarrier *carrier) {
if (!weakSelf) { return; }
__strong typeof(weakSelf) strongSelf = weakSelf;
ANCarrierMeta *newCarrierMeta = [ANCarrierMeta makeWithCarrier:carrier];
dispatch_async(dispatch_get_main_queue(), ^{
strongSelf.carrierMeta = newCarrierMeta;
});
};
CTCarrier *carrier = [self.networkInfo subscriberCellularProvider];
self.carrierMeta = [ANCarrierMeta makeWithCarrier:carrier];
}
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment