Showing posts with label DeviceInfo. Show all posts
Showing posts with label DeviceInfo. Show all posts
Delpin Susai Raj Tuesday, 21 August 2018

Xamarin.Forms- Device Display Info Using Xamarin Essentials

In this blog, you will learn how to get Device Display Information Using Xamarin Essentials in Xamarin forms. 


Introduction





Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.

Xamarin.Essentials 



Xamarin.Essentials plugin provides 20+ cross-platform APIs for  mobile application development. Xamarin.Essentials API works with all Xamarin.Forms, Xamarin.Android, Xamarin.iOS, or UWP application that can be accessed from shared code. When we are developing Xamarin with Android, iOS and UWP apps but now Xamarin.Essentials overcome the problem, developers can access every native platform API using C#. This plugin provides many APIs so initially, there is no need of more plugins for Xamarin. Xamarin.Essentials plugin impacts your app's minimum size.


Platform Support

Xamarin.Essentials supports platforms and operating systems

Platform      Version
Android      4.4 (API 19) or earlier
iOS             10.0 or higher
UWP     10.0.16299.0 or earlier

Prerequisites
  • Visual Studio 2017(Windows or Mac)
Setting up a Xamarin.Forms Project

Start by creating a new Xamarin.Forms project.  you’ll learn more by going through the steps yourself.
Choose the Xamarin.Forms App Project type under Cross-platform/App in the New Project dialog.



Name your app, select “Use Portable Class Library” for shared code, and target both Android and iOS.



You probably want your project and solution to use the same name as your app. Put it in your preferred folder for projects and click Create.



You now have a basic Xamarin.Forms app. Click the play button to try it out.



Setting up the User Interface

Go to MainPage.Xaml and write the following code.

MainPage.xaml

Add Xamarin Essentials


In this step, add Xamarin.Essentials to your project. You can install Xamarin.Essentials via NuGet, or you can browse the source code on GitHub.

Go to Solution Explorer and select your solution. Right-click and select "Manage NuGet Packages for Solution". Search "Xamarin.Essentials" and add Package. Remember to install it for each project (PCL, Android, iO, and UWP).




Xamarin.Essentials requires platform-specific setup

Android

The following steps are necessary for Android.
  1. Xamarin.Essentials supports a minimum Android version of 4.4
  2. Target Android version for compiling must be 8.1, API level 27.

In the Android project's MainActivity that is launched Xamarin.Essentials must be initialized in the OnCreate method.

MainActivity.cs

Xamarin.Essentials must receive any OnRequestPermissionsResult. write the following code for runtime permission.

MainActivity.cs

iOS

No additional setup required.

UWP

No additional setup required.


In this step, write the following code for get device display info.

MainPage.xaml.cs


using Xamarin.Forms;
using Xamarin.Essentials;
namespace XamarinEssentials
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            var metrics = DeviceDisplay.ScreenMetrics;

            lblOrientation.Text ="Orientation : " + metrics.Orientation.ToString();

            lblRotation.Text ="Rotation : " + metrics.Rotation.ToString();

            lblWidth.Text ="Width : " + metrics.Width.ToString();
            lblHeight.Text ="Height : " + metrics.Height.ToString();
            lblDensity.Text="Density : " + metrics.Density.ToString();
        }
       
    }
}




Click the play button to try it out.




Related post


I hope you have understood how to get Device Display  Info Using Xamarin Essentials in Xamarin.Forms.
Thanks for reading. Please share comments and feedback.
Delpin Susai Raj Thursday, 5 July 2018

Xamarin.Forms- DeviceInfo Using Xamarin Essentials

In this blog you will learn how to get DeviceInfo Using Xamarin Essentials in Xamarin forms. 


Introduction



Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.

Xamarin.Essentials 

Xamarin.Essentials provides developers with cross-platform APIs for their mobile applications.
Android, iOS, and UWP offer unique operating system and platform APIs that developers have access to all in C# leveraging Xamarin. Xamarin.Essentials provides a single cross-platform API that works with any Xamarin.Forms, Android, iOS, or UWP application that can be accessed from shared code no matter how the user interface is created.



When developing iOS and Android apps with Xamarin, developers can access every native platform API using C#. These bindings not only expose the platform APIs in C#, but add powerful C# features, such as async/await, events, delegates, and more. This is a huge advantage for developers, because they never have to leave C#, whether they’re writing shared business logic, user interface, or accessing native features. One key feature developers often look for when developing cross-platform apps with Xamarin is a way to access common native features from their shared code without having to write their own abstractions or find an open source plugin created by the community.


Xamarin.Essentials APIs

Xamarin.Essentials APIs provide access to over 25 native features from a single cross-platform API library, which can be accessed from shared code no matter how the user interface is created. This means you can use Xamarin.Essentials APIs with a single Xamarin.Android app or a Xamarin.Forms app targeting iOS, Android, and UWP. Even though it’s packed with features, it’s still fully optimized for performance and minimal impact on app size, because the library takes full advantage of being linker safe. This means only the APIs and features you use will be included in your app and the rest will be removed when you compile your app.

Platform Support

Xamarin.Essentials supports the following platforms and operating systems:

Platform     Version

Android     4.4 (API 19) or higher
iOS            10.0 or higher
UWP    10.0.16299.0 or higher

Prerequisites
  • Visual Studio 2017(Windows or Mac)
Setting up a Xamarin.Forms Project


Start by creating a new Xamarin.Forms project.  you’ll learn more by going through the steps yourself.
Choose the Xamarin.Forms App Project type under Cross-platform/App in the New Project dialog.



Name your app, select “Use Portable Class Library” for shared code, and target both Android and iOS.



You probably want your project and solution to use the same name as your app. Put it in your preferred folder for projects and click Create.



You now have a basic Xamarin.Forms app. Click the play button to try it out.



Setting up the User Interface

Go to MainPage.Xaml and write the following code.

MainPage.xaml

Add Xamarin Essentials

In this step, add Xamarin.Essentials to your project. You can install Xamarin.Essentials via NuGet, or you can browse the source code on GitHub.

Go to Solution Explorer and select your solution. Right-click and select "Manage NuGet Packages for Solution". Search "Xamarin.Essentials" and add Package. Remember to install it for each project (PCL, Android, iO, and UWP).





Xamarin.Essentials requires platform-specific setup

Android

Xamarin.Essentials supports a minimum Android version of 4.4, corresponding to API level 19, but the target Android version for compiling must be 8.1, corresponding to API level 27. (In Visual Studio, these two versions are set in the Project Properties dialog for the Android project, in the Android Manifest tab. In Visual Studio for Mac, they're set in the Project Options dialog for the Android project, in the Android Application tab.)

Xamarin.Essentials installs version 27.0.2 of the Xamarin.Android.Support libraries that it requires. Any other Xamarin.Android.Support libraries that your application requires should also be updated to version 27.0.2 using the NuGet package manager. All Xamarin.Android.Support libraries used by your application should be the same, and should be at least version 27.0.2. Refer to the troubleshooting page if you have issues adding the Xamarin.Essentials NuGet or updating NuGets in your solution.

In the Android project's MainLauncher or any Activity that is launched Xamarin.Essentials must be initialized in the OnCreate method:

To handle runtime permissions on Android, Xamarin.Essentials must receive any OnRequestPermissionsResult. Add the following code to all Activity classes:


iOS

No additional setup required.

UWP

No additional setup required.


In this step, write the following code for get Device Information.


MainPage.xaml.cs

using Xamarin.Forms;
using Xamarin.Essentials;
namespace XamarinEssentials
{
 public partial class MainPage : ContentPage
 {
  public MainPage()
  {
   InitializeComponent();
             
            lblDeviceName.Text ="Device Name :"+ DeviceInfo.Name;
            lblDeviceType.Text ="DeviceType :"+ DeviceInfo.DeviceType.ToString();
            lblModel.Text ="Model :"+ DeviceInfo.Model;
            lblManufacturer.Text ="Manufacturer :" + DeviceInfo.Manufacturer;
            lblPlatform.Text ="Platform : " + DeviceInfo.Platform;
            lblVersion.Text = "Version :" + DeviceInfo.Version;
  }
 }
}



Click the play button to try it out.



I hope you have understood how to get DeviceInfo Using Xamarin Essentials in Xamarin.Forms.
Thanks for reading. Please share comments and feedback.
Delpin Susai Raj Monday, 18 September 2017

How to Get Device Info(UDID, Model, Name, OS Version) in Xamarin iOS

In this article, you will learn how to Get Device Info(UDID, Model, Name, OS Version) in Xamarin iOS, using Xamarin Studio.

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

Download Code Here...




Prerequisites


  1. Xamarin Studio.
  2. Xcode.


The steps given below are required to be followed in order to Get Device Info(UDID, Model, Name, OS Version) in Xamarin iOS, using Xamarin Studio.

Step 1

Go To Xamarin Studio.
Click New Solution—> select Multiplatform—> Choose single View App. Native(iOS,Android). Afterwards, click Next.



Step 2

In This step Configure your app. Give App name(Ex: sample) and Organization Identifier. and Choose Target Platforms. Next, Choose Shared Code.Afterwards, click Next.



Step 3

In this step Give your Project Name(Ex: Sample). And Solution Name(EX: Sample). and give the path of your project. Afterwards, click Create.



Step 4

Subsequently, Go to Solution. In Solution, get all the files and source in your project. Now, select Main.storyboard double click to open Main.storyboard page.



Step 5

After opening Main.storyboard,  You can design this page, as per your desire.



Step 6

Now, go to the Toolbox Window. In the Toolbox Window, get all the types of the tools and controls. You need to go to the Toolbox Window. Now, scroll down and you will see all the tools and controls. You need to drag and drop the Label.



Step 7

You need to drag and drop four Labels .and align the Labels using Constraints.

  Constraints More info refer the following link.

https://developer.xamarin.com/guides/ios/user_interface/designer/designer_auto_layout/



Step 8

Now, go to the Properties Window. Select the First label and give Name(Ex:lblName)



Step 9

Select the Second label and give Name(Ex:lblUDID)



Step 10

Select the Third label and give Name(Ex:lblVersion)




Step 11

Select the Fourth label and give Name(Ex:lblDeviceName)




Step 12

In this step, go to the ViewController.cs page. write the code given below.

ViewController.cs

using System;

using UIKit;

namespace XamariniOSDeviceInfo.iOS
{
 public partial class ViewController : UIViewController
 {
  

  public ViewController(IntPtr handle) : base(handle)
  {
  }

  public override void ViewDidLoad()
  {
   base.ViewDidLoad();

   lblName.Text = UIDevice.CurrentDevice.Name;
   lblVersion.Text = UIDevice.CurrentDevice.SystemVersion;
   lblDeviceName.Text = UIDevice.CurrentDevice.Model;
   lblUDID.Text = UIDevice.CurrentDevice.IdentifierForVendor.ToString();

  }

  public override void DidReceiveMemoryWarning()
  {
   base.DidReceiveMemoryWarning();
   // Release any cached data, images, etc that aren't in use.  
  }
 }
}







Step 13

Now, Go to Run Option Choose Debug. and The List of iPhone, iPad Simulators Available, You can Choose any one and run it.



Output

After few seconds, the app will start running on your iPhone Simulator.You will see your app is work Successfully.



Summary

This was the process of how to Device Info(UDID, Model, Name, OS Version) in Xamarin iOS, using Xamarin Studio.

Please share your comments and feedback.