Are You Really Using Your Phone’s Camera in .NET MAUI? Most Aren’t Doing It Right!
Integrating native features into your .NET MAUI app isn’t just a nice-to-have. It’s often the very thing that transforms a basic app into something that feels truly native. Whether it’s capturing an image, pinpointing a user’s location, or tapping into sensors like the accelerometer, your app has the power to reach deep into platform-specific capabilities.
Let me show you how.
Using Platform APIs
.NET MAUI provides a rich abstraction layer over the native APIs of Android, iOS, macOS, and Windows. With it, you can access hardware and OS features without writing platform-specific code in most cases. These include interacting with the camera, retrieving geolocation, accessing sensors like the accelerometer, gyroscope, and more. Let’s look at a few real-world scenarios.
Accessing the Camera
Want to snap a photo directly from your app? Here’s how you use MediaPicker
:
var photo = await MediaPicker.CapturePhotoAsync();
if (photo != null)
{
var stream = await photo.OpenReadAsync();
// Use the stream to display or save the image
}
Explanation:
MediaPicker.CapturePhotoAsync()
invokes the native camera UI on the platform. You get back aFileResult
, which you can open as a stream and display in your app or upload.
You can also allow users to select an existing image using MediaPicker.PickPhotoAsync()
.
Accessing GPS
Use Geolocation
to get the device’s current location:
var location = await Geolocation.GetLastKnownLocationAsync();
if (location != null)
{
Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}");
}
Tip: For higher accuracy, use
GetLocationAsync
withGeolocationRequest
and specify accuracy and timeout.
This is particularly useful for delivery, fitness, or travel apps that rely on real-time location tracking.
Accessing the Accelerometer
You can tap into motion detection via the Accelerometer
:
Accelerometer.ReadingChanged += (s, e) =>
{
var data = e.Reading;
Console.WriteLine($"X: {data.Acceleration.X}, Y: {data.Acceleration.Y}, Z: {data.Acceleration.Z}");
};
Accelerometer.Start(SensorSpeed.UI);
Note: Always stop sensors when not needed to save battery:
Accelerometer.Stop()
.
This is excellent for detecting physical movement or shake gestures to trigger functionality, like undo actions or game controls.
Dependency Services and Platform Implementations
In some cases, you need access to APIs that aren’t yet abstracted by MAUI, or you need deeper customization. That’s where platform-specific implementations come in handy.
Platform-Specific Code with Partial Classes
Create platform-specific files using partial classes:
// Shared code
public partial class DeviceService
{
public partial string GetDeviceName();
}
// Android implementation
public partial class DeviceService
{
public partial string GetDeviceName()
{
return Android.OS.Build.Model;
}
}
// iOS implementation
public partial class DeviceService
{
public partial string GetDeviceName()
{
return UIKit.UIDevice.CurrentDevice.Name;
}
}
Why This Works: .NET MAUI includes the correct implementation at build time depending on the platform target.
This pattern ensures separation of concerns and keeps your shared code clean.
Dependency Injection with Interfaces
You can also use interfaces to inject platform-specific services:
// Interface
public interface IPlatformLogger
{
void Log(string message);
}
// Android
public class AndroidLogger : IPlatformLogger
{
public void Log(string message) => Android.Util.Log.Debug("APP", message);
}
// iOS
public class iOSLogger : IPlatformLogger
{
public void Log(string message) => Console.WriteLine($"iOS Log: {message}");
}
// Register
builder.Services.AddSingleton<IPlatformLogger, AndroidLogger>();
Benefit: Keeps your core code clean and testable by separating platform specifics.
This approach fits perfectly into an MVVM architecture and is compatible with dependency injection containers like Microsoft.Extensions.DependencyInjection.
Security and Permissions
Security isn’t just a checkbox — it’s foundational when working with native features. Any time you access hardware or personal data (location, contacts, camera), you must respect platform policies and protect user privacy.
Requesting Permissions
Always request permissions at runtime for sensitive features:
var status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
if (status != PermissionStatus.Granted)
{
// Handle denied access
}
Platform Notes: iOS requires describing permission usage in
Info.plist
, and Android needs entries inAndroidManifest.xml
.
Failure to declare or explain these permissions can lead to runtime crashes or rejected app submissions.
Protecting User Data
- Always access sensitive data via secure APIs (e.g., SecureStorage).
- Never store personal data unencrypted.
- Follow platform-specific guidelines for background access, location, etc.
await SecureStorage.SetAsync("token", "my_secret_token");
var token = await SecureStorage.GetAsync("token");
Reminder: SecureStorage works only when device lock is set.
Use platform-native keychains or secure storage options under the hood, abstracted nicely by MAUI. Combine this with data sanitization, encryption, and local authentication (like biometrics) for best results.
FAQ: Common Questions on Native Integration in .NET MAUI
Not always. Availability depends on the device and OS version.
Use real devices. Emulators often don’t support full sensor access (especially camera, GPS).
Respect the user’s decision. Notify clearly and offer alternatives when possible.
Conclusion: Make Your MAUI App Feel Like Home
Integrating native features in .NET MAUI is your ticket to building apps that feel right on each platform. From snapping pictures to detecting motion, MAUI’s abstraction makes it easy to tap into device power. But with great power comes the need for clean architecture and proper permission handling.
Next time you’re about to launch that app, ask yourself: “Have I made it truly native yet?”
Let me know what native feature you’d love to integrate next — or if you’ve hit a weird edge case!
- Introduction to .NET MAUI: What, Why, and How?
- .NET MAUI Architecture: Handlers & Platform Code
- Native Features in .NET MAUI: Camera, GPS, Sensors & Security
- Building UI with XAML and MVVM in .NET MAUI
- .NET MAUI Navigation: Shell, Tabs, Modals Explained