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>
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>
Share
Improve this question
asked Nov 19, 2024 at 8:32
FilloFillo
2674 silver badges17 bronze badges
2 Answers
Reset to default 2I 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>
}
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