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.
dotnet add package CurrentDevice
<PackageReference Include="CurrentDevice" Version="1.0.2" />
@using CurrentDevice
var builder = WebAssemblyHostBuilder.CreateDefault(args);
//... Shortend for brevity
builder.Services.AddCurrentDeviceService();
//... Shortend for brevity
await builder.Build().RunAsync();
@code{
[Inject] ICurrentDeviceService CurrentDeviceService { get; set; }
}
or
@inject ICurrentDeviceService CurrentDeviceService
protected override async Task OnInitializedAsync()
{
UserAgent = await CurrentDeviceService.GetUserAgent();
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
UserAgent = await CurrentDeviceService.GetUserAgent();
StateHasChanged();
}
}
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 returns string "landscape" or "portrait" | Method |
---|---|
Landscape | CurrentDeviceService.Landscape() |
Portrait | CurrentDeviceService.Portrait() |
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' |
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
MIT