Get Carrier Info

Jassim Al Rahma 1,551 Reputation points
2022-11-16T17:47:52.657+00:00

Hi,

How can I have a Dependency in MAUI to retrieve the carrier Info?

Thanks,
Jassim

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,199 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 38,906 Reputation points Microsoft Vendor
    2022-11-17T01:55:26.55+00:00

    Hello @Jassim Al Rahma ,

    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  
       }  
    

    You could refer to Invoke platform code to get more details.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful