If you have upgraded Xamarin.Forms from a version prior to V2.3.4, you will probably have a few warnings about the combination of Device.OS and TargetPlatform being obsolete.
The help text indicates that you should be using RuntimePlatform instead, but your first reaction will probably be, “But…that’s a string?” and it rather unhelpfully doesn’t give any examples of what to replace TargetPlatform with…
It’s an easy fix though and is a little tidier now. The Device class now has the following constants for you to use:
-
Device.iOS
Device.Android
Device.WinPhone
Device.Windows
Here’s a quick example. Say you want to add a header to your rest request with a different value for iOS and Android. Just switch on Device.RuntimePlatform:
switch (Device.RuntimePlatform) { case Device.Android: _restClient.HttpClient.DefaultRequestHeaders.Add("my-app-user-agent", "mobileapp-android"); break; case Device.iOS: _restClient.HttpClient.DefaultRequestHeaders.Add("my-app-user-agent", "mobileapp-ios"); break; }