Discovering the type of device - Desktop, Android, or IOS - in C# .Net Core Razor pages

Explanation:

Currently, my project involves using VS2022 and Net Core 9.0.

The goal is to implement a feature where users can send SMS to clients by clicking on a button.

Upon checking the device type, the app will select the appropriate syntax for sending the SMS based on whether it's Android or IOS.

However, the challenge lies in dealing with the different syntaxes required for Android and IOS platforms.

Question:

How can I determine if the device is Desktop, Android, or IOS? Are there methods available for this purpose?

Code:

For Android compatibility:

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

For iOS compatibility:

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

Answer №1

A helpful tool I've utilized in my Blazor projects is BlazorBrowserDetect. It has proven to be effective and user-friendly within the Blazor framework.

Check out BlazorBrowserDetect here

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

If you're working with Razor pages, incorporating the browserDetect.js file could also be advantageous: browserDetect.js

Answer №2

Here is my solution to the problem:

Step 0: Installing the Library

Start by installing the Shyjus.BrowserDetector package from Nuget.

Step 1: Adding code to Program.cs

Add the following line to your Program.cs file:
builder.Services.AddBrowserDetection();

Step 2: Backend Implementation

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;
}

Step 3: 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>

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble using XMLHttpRequest to pass a JavaScript variable to PHP for querying MySQL. Any solutions?

I'm attempting to retrieve data from my MySQL database by using JavaScript in my HTML file. The specific Javascript code I have is as follows: <script> function getGameNumberOfPlays() { var sentValue1 = Number; var xhttp; xhttp = ne ...

Each time the Angular children component is reloaded, the user is redirected to localhost:4200

In my Angular project, I encounter an issue with route parameters in children components. While navigating to these child components from the parent is seamless, reloading the child component causes the application to redirect to localhost:4200 and display ...

Is it possible for a d3 chart to render twice in one area if it's rendered in two different places?

When attempting to showcase two distinct d3 pie charts on my webpage within individual mat-cards, they both end up displaying in the svg tag of the first d3 chart in my code. This is what my code looks like: <section class="three"> <! ...

Securing User Profiles in Firebase

I am currently working on a coding issue that involves the security of user profiles. While it doesn't involve sensitive information like payment details or personal data, it does pertain to the ownership of a profile. Currently, I store users' ...

A guide on displaying a text file from a URL in a reactjs application

When utilizing the code provided below, I have successfully managed to display the content of a local text file on a webpage. However, when attempting to render a text file from a URL, the state becomes null resulting in an empty display. In order to ren ...

Is there an effortless method to invoke a function that alters the display from a higher-level parent component?

Currently, I am in the process of developing a webpage utilizing Ajax Tabs and user controls. The .aspx page includes a default control reference. <%@ Register src="~/Controls/DefaultControl.ascx" tagname="DefaultControl" tagprefix="uc1" %> <asp ...

The functionality of "Body Onload" for sending "ScrollHeight" is malfunctioning in Chrome and Safari

I came across an issue where the iframe_resize function in my code was not working as expected. After investigating further, I realized that the problem did not lie with the function itself. So, I decided to post a new question. Within my index.html file ...

What should I do to resolve the "identifier expected" issue?

Please refrain from being negative just because I can't ask more questions! Take a look at the image of my code here ...

Html code snippet: Trigger a popup message with custom content, then redirect to another

One of the requirements is to display a popup when a person clicks on a button (this functionality has been implemented using Html.ActionLink with older code; the popup will only show if a session variable equals a certain value, which has already been fig ...

The concept of Selenium PageObjects: breaking down web components into reusable modules

When implementing the PageObjects pattern to components of a page, what is considered the standard approach? For example, let's say we are writing tests for the various features on an Amazon product page. This page contains multiple separate feature ...

The AngularJS $filter(date) function is causing incorrect format outputs

Hey there! I've come across an issue with my AngularJS filter, where it's supposed to return a date in a specific format. However, when I try the following code: var input = '2015-08-11T13:00:00'; var format = 'yyyy MMM dd - hh:mm ...

Filtering an Array in VueJS Based on User Input

Currently, I am working on a Vue.js application where my goal is to filter an array based on user input from a form. The issue I am facing is that the array named autocomplete is not being populated with visitors that match the query of the first name. T ...

"Enhance User Experience by Keeping Silverlight Tooltips Visible on Mouseover

Hello and thank you for offering your assistance. Currently, I am working with a Treeview where I am using a Hierarchical data template to populate it. The bottom nodes in this Treeview display a tooltip that shows a small stack panel containing data relat ...

Converting XML data to SQL using C#

I am currently working on a C# application that will handle the import of approximately 26,000 XML records into a SQL Server database. These records will then be reformatted and imported into a new application. The database structure is already in place ...

Why doesn't the default value in a React select update the selected input value and title when changed during rendering?

Through several attempts, I have successfully developed a React select dropdown that displays dynamic data for each sector. However, I am facing an issue with setting a default value upon rendering. Although I am able to add a static default value like S ...

Tips on accessing a function or variable within a script executed using $.getScript

After running a script with $.getScript, is there a way to access the namespace of that script? I expected to be able to do so since the plugin function is defined in the global scope. index.js $.getScript('plugin.js').then((...result) => co ...

Can you provide guidance on securing a REST API using Google authentication?

I'm currently working on developing a REST API using NodeJS and Passport for a single-page JavaScript application. I am struggling to find the right approach to securing my REST API with Google OAuth. Can someone guide me on how to achieve this? Any ...

When users install my npm module, they are not seeing text prediction (intellisense) in their editors

I'm currently focused on developing my package @xatsuuc/startonomy. I'm encountering an issue where Intellisense is not working properly when the package is installed on the user's side. Even though the .d.ts files are visible in node_modul ...

Issue: Mail delivery unsuccessful in asp.net

I am facing an issue with sending emails. I enter a valid email address, but when I click the submit button, I receive the following error: Error: Failure sending mail. Below is the code from my web.config file: <system.net> <mailSettings> ...

JavaScript: Transform an Array of Strings into an Array of Objects

Here is an array containing different strings: let myArray : ["AA","BB" , "CC" ...] My goal is to transform it into an array of objects: myArray = [{"id":1 , "value": "AAA"},{"id":2 , "value": "BBB"},{"id":3 , "value": "CCC"}...] I attempted to achiev ...