mobile

.net Maui Detect Platform: Android And Ios Identification

Detecting the host operating system in a cross platform mobile application is essential when you need to branch logic, format user interfaces, or invoke native device APIs. In .NET Multi platform App

.net Maui Detect Platform: Android And Ios Identification
In this story

Detecting the host operating system in a cross-platform mobile application is essential when you need to branch logic, format user interfaces, or invoke native device APIs. In .NET Multi-platform App UI (.NET MAUI), developers can identify whether an app is running on Android or iOS within milliseconds using built-in APIs provided by the framework.

The primary mechanism for this task is the DeviceInfo class found in the Microsoft.Maui.Devices namespace. By querying DeviceInfo.Current.Platform, your app receives a DevicePlatform value that can be evaluated using standard conditional logic. This capability removes the boilerplate code traditionally required to hook into native operating system registries during startup.

Using DeviceInfo to Identify Android and iOS

The DeviceInfo class abstracts away the underlying operating system details, exposing them through clean C# properties. To check if your application is executing on an Android device or an iOS handset, you evaluate the Platform property against constants defined in the DevicePlatform structure.

Here is how you write a quick check inside your shared code project:

using Microsoft.Maui.Devices;

public void CheckRunningPlatform()
{
    if (DeviceInfo.Current.Platform == DevicePlatform.Android)
    {
        // Execute Android-specific logic
    }
    else if (DeviceInfo.Current.Platform == DevicePlatform.iOS)
    {
        // Execute iOS-specific logic
    }
}

This check executes instantly at runtime. Because DeviceInfo.Current is evaluated synchronously, you don't need to await any asynchronous tasks or manage complex initialization callbacks.

👉 See also: Galaxy A70 One UI

Practical Application Scenarios

Hardcoding platform checks everywhere creates maintenance debt, but certain architecture patterns require explicit platform detection. Understanding when and where to apply these checks ensures your codebase stays clean.

Adjusting User Interface Spacing and Padding

Different mobile operating systems handle status bars, notches, and home indicator bars differently. While layout abstractions handle most cases, you might want to modify padding or margins dynamically based on whether the app boots up on an Apple or Google ecosystem device.

Initializing Native Services

When configuring third-party push notification libraries, analytics packages, or secure storage handlers, initialization often requires passing native context or tokens. Detecting the platform early in your startup sequence lets you configure dependency injection containers correctly.

Alternative Approaches for Platform Abstraction

While simple if statements work for quick branches, professional architecture often favors design patterns that keep shared code decoupled from concrete operating systems.

📖 Related: Fly Or Die io:

Conditional Compilation Directives

If you need to include or exclude entire blocks of code or reference platform-specific namespaces like Android.OS or UIKit, use C# preprocessor directives.

#if ANDROID
// Android-specific namespace references allowed here
#elif IOS
// iOS-specific namespace references allowed here
#endif

The compiler strips out unused blocks during the build process, preventing compilation errors when building for mobile targets on desktop environments.

Partial Classes and Methods

Another clean architectural choice involves splitting a class definition across platform folders. You define a partial class with a partial method signature in your shared project, then implement that method inside the Platforms/Android and Platforms/iOS directories. The build system automatically stitches the correct file together depending on the compilation target.

You can learn more about structuring multi-targeted codebases by reading the official .NET MAUI invoking platform code documentation.

💡 You might also like: this post

Common Mistakes to Avoid

Developers transitioning from older cross-platform frameworks often stumble into a few predictable traps when querying device characteristics.

  • Querying before app initialization: Trying to access DeviceInfo.Current before the MAUI host has finished bootstrapping can result in a null reference exception. Always run platform checks inside your ViewModel constructors, page lifecycle methods, or after MauiApp.CreateMauiApp() completes.
  • Overusing platform checks for UI layout: Relying heavily on runtime platform checks to alter XAML styles makes code rigid. Instead, leverage XAML implicit styles, platform-specific handlers, or custom control mappers.
  • Confusing Platform with Idiom: DeviceInfo.Current.Platform returns the operating system (Android, iOS, WinUI), whereas DeviceInfo.Current.Idiom returns the device form factor (Phone, Tablet, Desktop). Don't check platform when you actually want to know if the user is holding a tablet or a phone.

For deep technical insights into cross-platform architectural design principles, consult the Microsoft .NET documentation hub.

Frequently Asked Questions

Can I detect platform types directly inside XAML markup?

Yes. MAUI XAML supports platform-specific markup extensions like OnPlatform and On which let you change property values or apply styles based on whether the app runs on Android or iOS without writing code-behind logic.

Does DeviceInfo require any special permissions in manifest files?

No permissions are required in your AndroidManifest.xml or Info.plist files to read basic runtime properties like platform, idiom, or device manufacturer through DeviceInfo.

Is platform detection performed at compile-time or run-time?

DeviceInfo.Current.Platform evaluates at runtime. If you need compile-time isolation to prevent assembly reference errors, use conditional compilation symbols like #if ANDROID or #if IOS instead.

IC

Written by

Isabella Carter

As a veteran correspondent, Isabella Carter has reported from across the globe, bringing firsthand perspectives to international stories and local issues.

More from mobile