最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if the device is Desktop, Android or IOS in C# .Net Core Razor pages? - Stack Overflow

programmeradmin1浏览0评论

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
Add a comment  | 

2 Answers 2

Reset to default 2

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>

}

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论