9. Qualsiasi cosa si possa fare in Objective-C
o Swift può essere fatta in C#
con Xamarin in Visual Studio
10. Xamarin è sempre aggiornato
Release iOS supportate al day-one:
iOS 5, iOS 6, iOS 7, iOS 7.1, iOS 8, iOS 9, iOS 10
Supporto ad Apple Watch!
11. Xamarin Studio
PC o Mac
Plugin Visual Studio
VS 2010 e superiore
Ambienti di Sviluppo
12. Integrazione in Visual Studio
Una soluzione sola per:
• iOS
• Android
• Windows Phone
• Windows Store
Tutti i plugin e le funzioni
di Visual Studio:
• ReSharper
• Team Foundation Server
13. Integrazione in Visual Studio
Debugging su:
• Emulatori
• Dispositivi
Integrati nella toolbar:
• Stato
• Logs
• Lista di dispositivi
14. Xamarin Studio
• Ottimizzato per lo sviluppo
cross-platform
• Accedi alle API native con
l’autocompletamento
• Designer per Android e iOS
• Debugging avanzato su
emulatore o dispositivo
15. Designer per Xamarin iOS
• Il primo designer iOS
presente in Xamarin Studio e
Visual Studio
• Simile al designer
tradizionale di Visual Studio
• Supporta pienamente l’UIKit
• Modifica i componenti
personalizzati e di terze parti
• Modifiche grafiche applicate
in tempo reale
16. Cosa impareremo oggi?
• Fundamentals
• The package
• Views and navigation
• Managing lists
• Working files
18. Before we start...
• You need a Mac:
– OS X 10.9.4+
– Xcode
– Xamarin.iOS
• You can still work on your Windows PC:
– Visual Studio
– Xamarin.iOS
19. Before we start...
• The OS X and Windows machines have to be connected
to the same network
• You can connect a OS X machine only to multiple
Windows machines
• OS X and Windows need to have the same Xamarin.iOS
version installed
20. iPhone simulator
• Requires the latest version of Xamarin.iOS.
• Allows you to “completely forget” about your
Mac.
• Fun fact: given that the Windows PC has a
touchscreen, it supports this kind of input.
21. What if I don’t have a Mac? Rent one!
• Always available.
• Pay as you go.
• Scalable according to our needs.
22. The application
• Main
– It’s the entry point of the app
– It creates the application
• AppDelegate
– It manages the system events
– It has an Application Window with the UI
– It starts the Main interface
23. The startup
• Storyboard
– Takes care of initializing the main controller
– The controller manages the main view and the sub views
• ViewController
– It receives the interactions
from the views
– It takes care of displaying
the data on the view
24. Apple Watch has Storyboards, too!
• Same functionalities you would expect from iPhone storyboards.
• As of today, easier to work with in terms of app rescaling.
25. Model-View-Controller (MVC)
• iOS is based on MVC
– The Model describes the data
– The View describes the UI
– The Controller manages the interactions
between the Model and the View
27. Domanda 1
Posso creare applicazioni iOS usando esclusivamente un PC Windows
a) Vero
b) Falso
28. Domanda 1
Posso creare applicazioni iOS usando esclusivamente un PC Windows
a) Vero
b) Falso
29. Domanda 2
Da quali componenti è composta un’applicazione iOS?
a) Main, AppDelegate
b) Window, AppDelegate
c) Main, ViewController
d) Screen, ViewController
30. Domanda 2
Da quali componenti è composta un’applicazione iOS?
a) Main, AppDelegate
b) Window, AppDelegate
c) Main, ViewController
d) Screen, ViewController
32. Resources
• iOS requires that all the resources are placed in the root
folder
• Xamarin adds a Resource folder
– At compile time, the content is copied in the root
• The Build Action has to be set to BundleResource
33. Images
• The original iPhone had a resolution of 320x480
• New resolutions have been added
• Naming conventions to manage the assets:
– @2x -> Retina
– @3x-> iPhone 6 Plus
– ~iphone -> Used on the iPhone
– ~ipad -> Used on the iPad
– Example: Image.png, Image@2x.png,
Image2x~iphone.png
34. Info.plist and Entitlements.plist
iPhone Deployment info
• It contains the main info about the app
configuration:
– Main interface
– Supported orientations
– Status bar style
Entitlements
• Used to configure iCloud,
push notifications, HealthKit, etc.
37. Domanda 1
Se volessi implementare iCloud nella mia app iOS, tramite quale file
dovrei abilitarne l’integrazione?
a) BundleResources.plist
b) Info.plist
c) CloudConnectivity.plist
d) Entitlements.plist
38. Domanda 1
Se volessi implementare iCloud nella mia app iOS, tramite quale file
dovrei abilitarne l’integrazione?
a) BundleResources.plist
b) Info.plist
c) CloudConnectivity.plist
d) Entitlements.plist
39. Storyboards
• In the past, iOS developers managed views using the
XIB files
• Each XIB files was mapped with a controller
• The developer needed to manually manage the
navigation between two views
40. Storyboards
• A storyboard is the visual representation of all the
screens in an app
• It’s made by different scenes
• A scene is a View managed by a ViewController
• Scenes contains objects and controls that are used to
manage interactions and to display data
41. The Xamarin designer
• Storyboards can’t be created in code
• The underline XML is generated automatically by
the designer
• Xamarin offers a built-in designer, which requires
a Mac Build Host connected to the Windows
machine
42. Navigation
• Navigation in Storyboards is managed using
Segues
• A Segue is a transition between two scenes
• Multiple transitions:
– Simple navigation
– Modal views
– Popover
• Segues are created using the designer
46. Domanda 1
Come si chiama il file contenente tutte le schermate della mia app iOS?
a) Segues
b) Storyboard
c) Designer
d) BuildHost
47. Domanda 1
Come si chiama il file contenente tutte le schermate della mia app iOS?
a) Segues
b) Storyboard
c) Designer
d) BuildHost
48. Domanda 2
Cosa uso per navigare tra le varie pagine della mia app iOS?
a) Navigator
b) Arrow
c) Transitioner
d) Segues
49. Domanda 2
Cosa uso per navigare tra le varie pagine della mia app iOS?
a) Navigator
b) Arrow
c) Transitioner
d) Segues
50. Passing data
• A storyboards offers a method called
PrepareForSegue() to override
• You get a reference to the destination control and
you pass the data into a property
public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
{
base.PrepareForSegue(segue, sender);
var controller = segue.DestinationViewController as MyPageController;
controller.TextToShow = MyPhoneNumber.Text;
}
51. Conditional navigation
• Sometimes you need to perform navigation only if a
specific condition is satisfied
• You can override the ShouldPerformSegue()
method
public override bool ShouldPerformSegue(string segueIdentifier, NSObject sender)
{
if (segueIdentifier == "SegueToMainPage")
{
if (PasswordTextField.Text == "password") return true;
else return false;
}
return base.ShouldPerformSegue(segueIdentifier, sender);
}
53. The view’s lifecycle
Methods intherited by the UIViewController class
• ViewDidLoad
– The controller has loaded the connected view
• ViewWillAppear
– The pagecontent is about to appear
• ViewWillDisappear
– The page content is about to disappear
• ViewDidAppear / ViewDidDisappear
– The page content is appeared / disappeared
54. Alerts
If you target a pre iOS 8 device, you need to use the
UIAlertView class
UIAlertView av = new UIAlertView("Title",
"message",
null,
"OK",
null);
av.Show();
55. Alerts
If you target an iOS 8+ device, you can use:
• UIAlertController to display an alert
• UIActionSheet to display an action sheet
57. Built-in applications
• Same URI-based approach that you find in Windows
Phone
• You use the OpenUrl() method to invoke a URL
NSUrl url = new NSUrl("http://www.xamarin.com");
if (UIApplication.SharedApplication.CanOpenUrl(url))
{
UIApplication.SharedApplication.OpenUrl(url);
}
59. Table Views
Many different classes and APIs to implement a list
• UITableView
– It’s the table that contains the rows to display
• UITableViewCell
– It represent a row of the list
• UITableViewSource
– It’s a specific Xamarin class that makes easier to implement a
UITableView
– On iOS, you would to implement separately the
UIViewTableDataSource and UITableViewDelegate classes
60. Table Views
Many different classes and APIs to implement a list
• NSIndexPath
– It identifies a specific element of the table
• UITableViewController
– It’s the controller that implements an UITableView
61. Table layout
• Four different built-in styles
• To implement a custom layout, you need to implement a
custom cell
62. Creating a UITableViewSource
• You need to choose a cell identifier
• You need to implement the GetCell() method to define the
layout of a cell
public class TableSource : UITableViewSource
{
string[] tableItems;
string cellIdentifier = "TableCell";
public TableSource(string[] items)
{
tableItems = items;
}
public override nint RowsInSection(UITableView tableview, nint section) { }
public override UITableViewCell GetCell(UITableView tableView, Foundation.NSIndexPath
indexPath)
{
...
}
}
63. Cell reuse
• It’s the technique used to improve performances in case
of big collections to display
• You first ask for a cell to reuse before creating a new one
public override UITableViewCell GetCell(UITableView tableView, Foundation.NSIndexPath
indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell(cellIdentifier);
// if there are no cells to reuse, create a new one
if (cell == null)
cell = new UITableViewCell(UITableViewCellStyle.Default, cellIdentifier);
cell.TextLabel.Text = tableItems[indexPath.Row];
return cell;
}
64. Cell reuse
Starting from iO6, there’s a new method that automatically
takes care of generating the proper cell for you
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath
indexPath)
{
var cell = (MyCell)tableView.DequeueReusableCell(MyCellId, indexPath);
return cell;
}
68. Domanda 1
Di quali metodi e controlli mi posso avvalere per creare una lista in
un’app iOS?
a) UITableView
b) UITableViewContainer
c) UITableViewSource
d) UITableViewCell
69. Domanda 1
Di quali metodi e controlli mi posso avvalere per creare una lista in
un’app iOS?
a) UITableView
b) UITableViewContainer
c) UITableViewSource
d) UITableViewCell
74. Using a custom layout
• You need to create a custom cell
• You can do it in code or with the designer
• You add the controls inside
the UIViewTableCell object
• In code, you assign a value to the controls
75. Using a custom layout
In the UITableViewSource, you use the custom cell class
instead of the base UITableViewCell one in the GetCell()
method
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
CharacterCellView cell = tableView.DequeueReusableCell(CellIdentifier, indexPath) as
CharacterCellView;
Character currentCharacter = tableItems[indexPath.Row];
cell.UpdateCharacter(currentCharacter);
return cell;
}
78. Files and folders
• To create files and folder you can leverage the basic .NET
APIs (System.IO)
• Documents is the base path where all the files of the
application are stored
• In the info.plist you can
set the option
UIFileSharingEnabled
to true to allow access
to local files through
iTunes
79. The folders
• Library
– To store the internal files of the application
– It’s automatically backed up
• Library/Preferences
– To store the application settings
– It’s automatically backed up
• Library/Caches
– iOS can delete the content in case the space is going low
– It’s not backed up
We see here the Xamarin approach we talked about earlier
This enables you to be highly productive, share code, but build out UI on each platform and access platform APIs
With Xamarin.Forms you now have a nice Shared UI Code layer, but still access to platform APIs
You can start from native, pick a few screens, or start with forms, and replace with native later
Lo stesso avviene quindi per iOS e Android, mentre si usa Xamarin. Tutte le API native di iOS sono state coperte da Xamarin in C#.
Grazie alla developer preview di Apple, Xamarin è stata in grado di rilasciare il support fin da subito per queste release di iOS.
Google però non offre una developer preview, e quindi bisogna aspettare circa 4-8 settimane per avere una release stabile.
E’ importante notare come il supporto a dispositivo come Android Wear e Apple Watch si esprima anche con il debugging da appositi emulatori realizzati da Xamarin.
Xamarin Studio
PC -> Android
Mac -> iOS, Android, Mac
Visual Studio: iOS, Android Windows
Il plugin supporta Visual Studio 2010, 2012, 2013 e 2015
Supporta le desktop app su Windows: WPF, ASP.NET, Silverlight, WinForms
Soluzioni e progetti possono esssere aperti indistintamente in Xamarin Studio e Visual Studio
AGGIORNARE SCREEN
Xamarin Studio su PC consente solamente Android, su Mac Android, iOS e Mac.
Xamarin Studio consente di lavorare con Git e altri sistemi di source control.