skip to Main Content

Explaination:

I am currently working on VS2022 and Net Core 9.0.

I want to be able to send SMS to the client by clicking on a button.

After the App checks the device type, it selects the right button.

My problem is that Android and IOS have different syntaxes.

Question:

How can i detect if the device is Desktop, Android or IOS? Is there a method to do this?

Code:

To work on Android:

<a href="sms:/* phone number here */?body=/* body text here */">Link</a>

To work on iOS:

<a href="sms:/* phone number here */&body=/* body text here */">Link</a> 

2

Answers


  1. Chosen as BEST ANSWER

    I managed to solve it this way:

    0. Library Installation:

    Install : Shyjus.BrowserDetector, from the Nuget
    

    1. On Program.cs add:

    builder.Services.AddBrowserDetection();
    

    2.The backend code:

           private readonly IBrowserDetector browserDetector; 
       private readonly TEST.Models.DbContext _context;
    
       public IndexModel(my.DbContext context, IBrowserDetector browserDetector)
       {
           _context = context;
           this.browserDetector = browserDetector;
       }
    
       [TempData]
       public string GetOS { get; set; } = default!;
    
    
       public async Task OnGetAsync()
       {
           GetOS = browserDetector?.Browser?.OS;
       }
    

    3.The Frontend code:

    @{
    
    bool ooss = false;
    if (Model.GetOS == "Mac OS" || Model.GetOS == "iOS")
    {
        ooss = true;
    }else
    {
    
    }
    

    }

    Check if it is IOS:

    @if (ooss == true) {

    <a href="tel:+112345678&body=" class="btn btn-sm btn-outline-dark " >
        IOS Sms
    </a>
    

    } else {

    <a href="sms:+112345678?body=" class="btn btn-sm btn-outline-dark " >
        Android SMS
    </a>
    

    }


  2. I have been using BlazorBrowserDetect for this.
    It has worked pretty well for me and is easy to use, in Blazor.

    BlazorBrowserDetect

    <BrowserDetect BrowserInfoChanged="OnBrowserInfoChanged"/>
    @code {
       private void OnBrowserInfoChanged(BrowserInfo info)
       {
          var isMobile = info?.IsMobile ?? true;
          var isIPhone = info?.IsIPhone ?? false;
       }
    }
    

    In Razor, the browserDetect.js file might be helpful:
    browserDetect.js

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search