Skip to content

Repository files navigation

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

varbuilder=WebAssemblyHostBuilder.CreateDefault(args);//... Shortend for brevitybuilder.Services.AddCurrentDeviceService();//... Shortend for brevityawaitbuilder.Build().RunAsync();

Inject the service in your component

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

or

@injectICurrentDeviceService CurrentDeviceService

Usage in your component

Blazor WASM

protectedoverrideasyncTaskOnInitializedAsync(){UserAgent=awaitCurrentDeviceService.GetUserAgent();}

Blazor Server

protectedoverrideasyncTaskOnAfterRenderAsync(boolfirstRender){if(firstRender){UserAgent=awaitCurrentDeviceService.GetUserAgent();StateHasChanged();}}

Device Methods

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

CurrentDeviceService: Returns True/FalseMethod
MobileCurrentDeviceService.Mobile()
TabletCurrentDeviceService.Tablet()
DesktopCurrentDeviceService.Desktop()
iOSCurrentDeviceService.iOS()
iPadCurrentDeviceService.iPad()
iPhoneCurrentDeviceService.iPhone()
iPodCurrentDeviceService.iPod()
AndroidCurrentDeviceService.Android()
Android PhoneCurrentDeviceService.AndroidPhone()
Android TabletCurrentDeviceService.AndroidTablet()
BlackBerryCurrentDeviceService.Blackberry()
BlackBerry PhoneCurrentDeviceService.BlackberryPhone()
BlackBerry TabletCurrentDeviceService.BlackberryTablet()
WindowsCurrentDeviceService.Windows()
Windows PhoneCurrentDeviceService.WindowsPhone()
Windows TabletCurrentDeviceService.WindowsTablet()
MacOsCurrentDeviceService.MacOs()
MeeGoCurrentDeviceService.MeeGo()
TelevisionCurrentDeviceService.Television()

Orientation Methods

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

Useful Methods

MethodReturns
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

About

100% C# implementation of CurrentDevice.js

Resources

Stars

4 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages