Skip to content

Commit

Permalink
feat: handled retrieving carrier names as per different iOS versions
Browse files Browse the repository at this point in the history
  • Loading branch information
desusai7 authored and Desu Sai Venkat committed May 9, 2023
1 parent 11cd359 commit a81e3a2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
12 changes: 6 additions & 6 deletions Sources/Classes/Headers/Public/RSNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
NS_ASSUME_NONNULL_BEGIN

@interface RSNetwork : NSObject
- (instancetype) initWithDict:(NSDictionary*) dict;
- (NSDictionary<NSString* , NSObject *>*) dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
- (NSDictionary<NSString *, NSObject *> *)dict;

@property (nonatomic, readwrite) NSString* carrier;
@property (nonatomic, readwrite) bool wifi;
@property (nonatomic, readwrite) bool isNetworkReachable;
@property (nonatomic, readwrite) bool cellular;
@property(nonatomic, strong) NSMutableArray<NSString *> *carrier;
@property(nonatomic, readwrite) bool wifi;
@property(nonatomic, readwrite) bool isNetworkReachable;
@property(nonatomic, readwrite) bool cellular;

@end

Expand Down
38 changes: 32 additions & 6 deletions Sources/Classes/RSNetwork.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
//

#import "RSNetwork.h"

#import "RSLogger.h"
#import "RSUtils.h"
#if !TARGET_OS_TV && !TARGET_OS_WATCH
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
Expand All @@ -19,12 +20,35 @@ - (instancetype)init
{
self = [super init];
if (self) {
_carrier = [[NSMutableArray alloc] init];
#if !TARGET_OS_TV && !TARGET_OS_WATCH
NSString *carrierName = [[[[CTTelephonyNetworkInfo alloc] init] subscriberCellularProvider]carrierName];
if (carrierName == nil) {
carrierName = @"unavailable";
CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
if(@available(iOS 16.0, *)) {
[RSLogger logWarn:@"RSNetwork: init: cannot retrieve carrier names on iOS 16 and above as CTCarrier is deprecated"];
}
else if (@available(iOS 12.0, *)) {
NSDictionary *serviceProviders = [networkInfo serviceSubscriberCellularProviders];
for (NSString *rat in serviceProviders) {
CTCarrier *carrier = [serviceProviders objectForKey:rat];
if(carrier == nil) {
continue;
}
NSString *carrierName = [carrier carrierName];
if (carrierName && ![carrierName isEqualToString:@"--"]) {
[_carrier addObject:carrierName];
}
}
} else {
CTCarrier *carrier = [networkInfo subscriberCellularProvider];
NSString *carrierName = [carrier carrierName];
if (carrierName) {
[_carrier addObject:carrierName];
}
}

if(_carrier.count == 0) {
[RSLogger logWarn:@"RSNetwork: init: unable to retrieve carrier name"];
}
_carrier = carrierName;
#endif
#if !TARGET_OS_WATCH
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, "8.8.8.8");
Expand Down Expand Up @@ -60,7 +84,9 @@ - (instancetype) initWithDict:(NSDictionary*) dict {
NSMutableDictionary *tempDict;
@synchronized (tempDict) {
tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:_carrier forKey:@"carrier"];
if(_carrier.count !=0) {
[tempDict setValue:[RSUtils getCSVString:_carrier] forKey:@"carrier"];
}
#if !TARGET_OS_WATCH
if(_isNetworkReachable) {
[tempDict setValue:[NSNumber numberWithBool:_wifi] forKey:@"wifi"];
Expand Down
9 changes: 1 addition & 8 deletions Sources/Classes/RSUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,7 @@ + (NSArray*) serializeArray:(NSArray*) array {
}

+(NSString*) getCSVString:(NSArray*) inputStrings {
NSMutableString *CSVString = [[NSMutableString alloc] init];
for (int index = 0; index < inputStrings.count; index++) {
[CSVString appendString:inputStrings[index]];
if (index != inputStrings.count -1) {
[CSVString appendString:@","];
}
}
return [CSVString copy];
return [inputStrings componentsJoinedByString:@","];
}

+(NSString*) getJSONCSVString:(NSArray*) inputStrings {
Expand Down
5 changes: 5 additions & 0 deletions Tests/RudderUtilsTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class RudderUtilsTest: XCTestCase {
let bioCSV = RSUtils.getCSVString(bio)
print(bioCSV)
XCTAssert(bioCSV == "Desu,Mobile Engineer,RudderStack")
let carriers:[String] = ["Airtel"]
let carriersCSV = RSUtils.getCSVString(carriers)
print(carriersCSV)
XCTAssertEqual(carriersCSV, "Airtel")

}

func testGetJSONCSVString() throws {
Expand Down

0 comments on commit a81e3a2

Please sign in to comment.