In the development of mobile applications, there is a wide variety of alternatives for the creation of Software for iOS and Android. In the case of iOS, we have Swift and Objective C as initial options, however, if we want to take advantage of the C# language to create high-performance native applications, we can opt for Xamarin.iOS and .NET for iOS, the latter with the advantage of the framework of .NET 7. .NET for iOS is based on a core execution framework of .NET Core, which provides high performance in the execution of processes and when combined with the native interface of iOS, the performance is by far, far superior to any application that does rendering-compiling processes like React Native, Flutter, etc.
When to use .NET for iOS or Xamarin.iOS?
The use of Xamarin.iOS or .NET for iOS is when building robust applications with high performance is required and where performance is key beyond cross-platform.
"Xamarin.iOS is for competitive markets, where you must have a differentiator in performance" Miguel De Icaza, Xamarin Dev Days CDMX Virtual 2019.
The evolution of Xamarin.iOS is called .NET for iOS, which provides a runtime core based on .NET Core, as opposed to Xamarin.iOS where the foundation meets Mono Framework, the inclusion of .NET Core in development for iOS gives you superior performance in running applications.
Xamarin.iOS and .NET for iOS have a similar programming structure, where the native graphical interface is used with the XCode Interface Builder and CSharp programming, this provides pixel-by-pixel detail for the location of the elements, in addition to the fact that it can be linked with great performance to technologies such as Unity without latency in the execution of processes that require greater computing capacity than iOS.
Without a doubt, .NET for iOS is the right evolution of Xamarin.iOS and gives software developers the opportunity to create great iOS apps using C# as the programming language and the full power of the new Microsoft .NET 7 Framework.
How to make an iOS app using .NET 7?
Step 1
The first thing we will notice in Visual Studio 2022 for Mac is that the .NET for iOS template is in the iOS area, and where we can also find Xamarin.iOS as a second option, however in both cases we can create applications under the same structure, the graphic interface in the Xcode Interface Builder, or also directly in C# we can create the interface, as shown in the base example of .NET for iOS.
Step 2
We put the name to our application, and we locate the path where the project will be saved.
Step 3
Once the project is created, in the AppDelegate.cs file we will find the FinishedLaunching method, in that area we will locate the example code that creates a graphical interface with the message "Hello, iOS" and loads it as the main view of the application.
Step 4
When we run the application, the message will appear on the screen in the central part, this is one of the ways for the Graphical Interface to appear in the application.
Step 5
In order to take advantage of the full potential of the graphic interface, we proceed to prepare the environment for the use of the XCode Interface Builder and facilitate the location of the elements, use the Storyboard among other advantages of using the native Apple graphic interface. On the project name we right-click, add and select new class.
Step 6
From there in the iOS area we select iOS View Controller and we name it "ViewController" and we create.
Step 7
Of the files that appear, we eliminate those with the .XIB extension, because we will use a Main.Storyboard to have all the views in a single controller object for them.
Step 9
Again we right-click on the project name and add a new file.
Step 10
From the files we select “iOS StoryBoard” and we name it “Main”, which will be who has the elements of the Interface.
Step 11
In case an error appears, we accept it, since later we will proceed to link the storyboard with its viewcontroller.
Step 12
Now we right click on Main.Storyboard, open with and select XCode Interface Builder and wait for it to load the XCode.
Step 13
Once the XCode is loaded, we select the Main.Storyboard and click on the “+” symbol located in the upper right part.
Step 14
A menu of tools will appear, from where we will select ViewController.
Step 15
Our Graphical Interface for iOS will appear, from there we will select in the 4th. Option of the menu on the upper right side the area that says Custom Class, and from there in the class area, we select ViewController, with that our StoryBoard is connected with the ViewController class.
Step 16
In the 5th Menu option we can change the attributes or properties of the view, such as the color in the Background area.
Step 17
Again in the “+” symbol in the toolbox, we will select a label and a button, which we will place on the main screen, in the same way we can change its attributes and properties in the menu on the right side, change the font size, type, color, etc.
Step 19
In the 5th zone of the window, where it says "Simulated Metrics" we locate the option "Is Initial View Controller", there we can indicate that the view that we added will be the first window of our application (in addition to the LauchScreen that comes by default ).
Step 20
Below the “+” symbol is an icon that has a “+” with a small strip on the left side, clicking on it will divide the screen into two zones.
Step 21
In the area on the right side is the interface, in the area on the left side where it says Main.Storyboard when clicked, the file tree will appear, where we can find the "ViewController.h" file, which we selected.
Step 22
Once the ViewController is loaded, we select the button and right-click on it and select “New Referencing Outlet”, we take the point on the right side and leave it pressed, and we drag the point to the area of the code between the braces of the Interface of UIViewController.
Step 23
When released, a floating window will appear where we will put the name of the text box, and we locate that its Storage is as Strong to be able to map it from Visual Studio. We do the same process with the label where we put a name. Later we save the project in XCode.
Step 24
We return to Visual Studio for Mac, and we locate the ViewController.designer.cs file and we will find the added elements in the XCode graphical interface, with this we confirm the effective communication between Visual Studio and XCode back and forth.
Step 25
Now we proceed to prepare the ViewController class by adding the Intptr handle and base:(handle) to enable the loading of the objects of the elements of the graphical interface.
Step 26
In the AppDelegate.cs we comment out all the code, except for the return true, so that it doesn't load the C# coding interface.
Step 27
Now we must tell our application that we have a StoryBoard to start, for this we go to the Info.plist and from there we locate where it says "Add new entry", and from the menu we select Main storyboard file base name and we put on the right side the value with the name “Main”.
Step 28
Now we proceed to program, for this in the ViewController.cs which is our main class, in the ViewDidLoad method we place the following code:
Btn.TouchUpInside += delegate{
Lbl.Text = “Hello World .NET for iOS”;
};
Step 29
We run our application and that's it, we have our first application for iOS using .NET 7 and from there we can access the native iOS APIs, access to SQLite databases, access to APIs in Azure, etc, in addition to the full potential of .NET 7 on iOS.
Thank you very much.