MAUI Essentials Get Mobile Carrier Name

114 views Asked by At

We are currently building a mobile app using .NET 6 and are looking for a way to detect the name of the mobile carrier that a device is using.

Is this possible via MAUI Essentials?

If it isn't is it possible using native iOS and Android APIs? It seems that in iOS 16 Apple have made some changes to the availability of this information.

1

There are 1 answers

2
Jessie Zhang -MSFT On

You could refer to the following steps to invoke platform code as Dependency service on Xamarin.Forms.

Step 1: Implement the function to get Carrier Info for Android and iOS:

For Android:

Add the following code into your MainActivity.

public static MainActivity Instance { get; private set; }  
   protected override void OnCreate(Bundle savedInstanceState)  
   {  
       base.OnCreate(savedInstanceState);  
       Instance = this;  
   }

Create the helper class to get carrier info.

public static class GetCarrierHelper  
   {  
       public static string GetCarrierName()  
       {  
           TelephonyManager manager = MainActivity.Instance.GetSystemService(Context.TelephonyService) as TelephonyManager;  
           String carrierName = manager.NetworkOperatorName;  
           return carrierName;  
       }  
   }

For iOS, you need to create the following helper class:

public static class GetCarrierHelper  
   {  
       public static string GetCarrierName()  
       {  
           using (var info = new CTTelephonyNetworkInfo())  
           {  
               CTCarrier cTCarrier = (CTCarrier)info.ServiceSubscriberCellularProviders.ValueForKey((NSString)info.DataServiceIdentifier);  
               // You could get the CTCarrier according to DataServiceIdentifier key, then return the CarrierName. CTCarrier and its CarrierName property was deprecated  
               return cTCarrier.CarrierName;  
           }  
       }  
   }

Step 2: Invoke your platform code:

private async void Button_Clicked(object sender, EventArgs e)  
   {  
   #if ANDROID  
           var name = MauiApp9.Platforms.Android.GetCarrierHelper.GetCarrierName();  
   #elif IOS  
       var name = MauiApp9.Platforms.iOS.GetCarrierHelper.GetCarrierName();  
   #endif  
   }

For more informaiton, you can check Invoke platform code.