Skip to content

Latest commit

 

History

History
213 lines (178 loc) · 4.78 KB

README.md

File metadata and controls

213 lines (178 loc) · 4.78 KB

CurrentDevice

Device/User agent detector made in Blazor for Blazor. This library is a port of CurrentDevice

No need to use JavaScript interop to detect the device or user agent. This library is a pure C# implementation of the original library.

This library is trimmable and does not rely on Javascript (no need to add a <script> tag somewhere)

Warning

This library is still in development and may not be stable or working as you expect. Please use with caution.

Installation

NuGet Downloads

Nuget CLI

dotnet add package CurrentDevice

Csproj

<PackageReference Include="CurrentDevice" Version="1.0.3" />

Add reference in _Imports.razor

@using CurrentDevice

Usage

Add the service in your services method

var builder = WebAssemblyHostBuilder.CreateDefault(args);
//...  Shortend for brevity
builder.Services.AddCurrentDeviceService();
//... Shortend for brevity
await builder.Build().RunAsync();

Inject the service in your component

@code{
    [Inject] ICurrentDeviceService CurrentDeviceService { get; set; }
}

or

@inject ICurrentDeviceService CurrentDeviceService

Usage in your component

Blazor WASM

protected override async Task OnInitializedAsync()
{
    UserAgent = await CurrentDeviceService.GetUserAgent();
}

Blazor Server

protected override async Task OnAfterRenderAsync(bool firstRender)
{
    if (firstRender)
    {
        UserAgent = await CurrentDeviceService.GetUserAgent();
        StateHasChanged();
    }
}

Device Methods

To see a real world example you can visit the example here on Github

CurrentDeviceService: Returns True/False Method
Mobile CurrentDeviceService.Mobile()
Tablet CurrentDeviceService.Tablet()
Desktop CurrentDeviceService.Desktop()
iOS CurrentDeviceService.iOS()
iPad CurrentDeviceService.iPad()
iPhone CurrentDeviceService.iPhone()
iPod CurrentDeviceService.iPod()
Android CurrentDeviceService.Android()
Android Phone CurrentDeviceService.AndroidPhone()
Android Tablet CurrentDeviceService.AndroidTablet()
BlackBerry CurrentDeviceService.Blackberry()
BlackBerry Phone CurrentDeviceService.BlackberryPhone()
BlackBerry Tablet CurrentDeviceService.BlackberryTablet()
Windows CurrentDeviceService.Windows()
Windows Phone CurrentDeviceService.WindowsPhone()
Windows Tablet CurrentDeviceService.WindowsTablet()
MacOs CurrentDeviceService.MacOs()
MeeGo CurrentDeviceService.MeeGo()
Television CurrentDeviceService.Television()

Orientation Methods

Orientation returns string "landscape" or "portrait" Method
Landscape CurrentDeviceService.Landscape()
Portrait CurrentDeviceService.Portrait()

Useful Methods

Method Returns
CurrentDeviceService.Type() 'mobile', 'tablet', 'desktop', or 'unknown'
CurrentDeviceService.Orientation() 'landscape', 'portrait', or 'unknown'
CurrentDeviceService.OS() 'ios', 'iphone', 'ipad', 'ipod', 'android', 'blackberry', 'windows', 'macos', 'meego', 'television', or 'unknown'

Technical information

Lifetimes

Even though in DI it get added as scoped, blazor WASM will treat it as a singleton more on that here meaning that if an user changes User agents and refreshes the page it'll still display old data untill a page refresh

For blazor server it is scoped and every page request will have up to date information, interal responses get cached clientside per request so that if you check for Ipad then iOs it'll save some requests to the browser

License

MIT