81
Creare app native su iOS, Android, Mac & Windows in C# Introduzione a Xamarin.iOS

Xamarin.iOS introduction

Embed Size (px)

Citation preview

Page 1: Xamarin.iOS introduction

Creare app native su iOS, Android, Mac & Windows in C#

Introduzione a Xamarin.iOS

Page 2: Xamarin.iOS introduction

Gli speaker di oggi

Guido MagrinXamarin & Microsoft Student Partner

Xamarin Certified Developer

@GuidoMagrin

Page 3: Xamarin.iOS introduction

Dove trovo le slide?

http://www.slideshare.net/guidomagrin

Page 4: Xamarin.iOS introduction

Gli Xamarin Student Partner

https://www.facebook.com/XSAMilano

Page 5: Xamarin.iOS introduction

Oggi parleremo di…

Xamarin.iOS

Page 6: Xamarin.iOS introduction

Chi ha giàsentito parlare diXamarin.iOS?

Page 7: Xamarin.iOS introduction

Xamarin + Xamarin.FormsApproccio offerto da Xamarin.Forms: Codice UI condiviso, controlli nativi

Approccio tradizionale di Xamarin

Shared UI Code

Page 8: Xamarin.iOS introduction

.NET + iOS APIs | copertura al 100%

Page 9: Xamarin.iOS introduction

Qualsiasi cosa si possa fare in Objective-Co Swift può essere fatta in C#con Xamarin in Visual Studio

Page 10: Xamarin.iOS introduction

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!

Page 11: Xamarin.iOS introduction

Xamarin StudioPC o Mac

Plugin Visual Studio VS 2010 e

superiore

Ambienti di Sviluppo

Page 12: Xamarin.iOS introduction

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

Page 13: Xamarin.iOS introduction

Integrazione in Visual Studio

Debugging su:• Emulatori• Dispositivi

Integrati nella toolbar:• Stato• Logs• Lista di dispositivi

Page 14: Xamarin.iOS introduction

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

Page 15: Xamarin.iOS introduction

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

Page 16: Xamarin.iOS introduction

Cosa impareremo oggi?

• Fundamentals• The package• Views and navigation• Managing lists• Working files

Page 17: Xamarin.iOS introduction

Fundamentals

Page 18: Xamarin.iOS introduction

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

Page 19: Xamarin.iOS introduction

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

Page 20: Xamarin.iOS introduction

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.

Page 21: Xamarin.iOS introduction

What if I don’t have a Mac? Rent one!

• Always available.• Pay as you go.• Scalable according to our needs.

Page 22: Xamarin.iOS introduction

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

Page 23: Xamarin.iOS introduction

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

Page 24: Xamarin.iOS introduction

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.

Page 25: Xamarin.iOS introduction

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

Page 26: Xamarin.iOS introduction

La prima appXamarin.Forms

Page 27: Xamarin.iOS introduction

Domanda 1

Posso creare applicazioni iOS usando esclusivamente un PC Windows

a) Verob) Falso

Page 28: Xamarin.iOS introduction

Domanda 1

Posso creare applicazioni iOS usando esclusivamente un PC Windows

a) Verob) Falso

Page 29: Xamarin.iOS introduction

Domanda 2

Da quali componenti è composta un’applicazione iOS?a) Main, AppDelegateb) Window, AppDelegatec) Main, ViewControllerd) Screen, ViewController

Page 30: Xamarin.iOS introduction

Domanda 2

Da quali componenti è composta un’applicazione iOS?a) Main, AppDelegateb) Window, AppDelegatec) Main, ViewControllerd) Screen, ViewController

Page 31: Xamarin.iOS introduction

The package

Page 32: Xamarin.iOS introduction

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

Page 33: Xamarin.iOS introduction

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, [email protected],

Image2x~iphone.png

Page 34: Xamarin.iOS introduction

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.

Page 35: Xamarin.iOS introduction

Views andnavigation

Page 36: Xamarin.iOS introduction

La prima appXamarin.Forms

Page 37: Xamarin.iOS introduction

Domanda 1

Se volessi implementare iCloud nella mia app iOS, tramite quale file dovrei abilitarne l’integrazione?

a) BundleResources.plistb) Info.plistc) CloudConnectivity.plistd) Entitlements.plist

Page 38: Xamarin.iOS introduction

Domanda 1

Se volessi implementare iCloud nella mia app iOS, tramite quale file dovrei abilitarne l’integrazione?

a) BundleResources.plistb) Info.plistc) CloudConnectivity.plistd) Entitlements.plist

Page 39: Xamarin.iOS introduction

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

Page 40: Xamarin.iOS introduction

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

Page 41: Xamarin.iOS introduction

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

Page 42: Xamarin.iOS introduction

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

Page 43: Xamarin.iOS introduction

Storyboards, Segues and Xamarin designer

Page 44: Xamarin.iOS introduction

Storyboards and segues

DEMO

Page 45: Xamarin.iOS introduction

La prima appXamarin.Forms

Page 46: Xamarin.iOS introduction

Domanda 1

Come si chiama il file contenente tutte le schermate della mia app iOS?

a) Seguesb) Storyboardc) Designerd) BuildHost

Page 47: Xamarin.iOS introduction

Domanda 1

Come si chiama il file contenente tutte le schermate della mia app iOS?

a) Seguesb) Storyboardc) Designerd) BuildHost

Page 48: Xamarin.iOS introduction

Domanda 2

Cosa uso per navigare tra le varie pagine della mia app iOS?a) Navigatorb) Arrowc) Transitionerd) Segues

Page 49: Xamarin.iOS introduction

Domanda 2

Cosa uso per navigare tra le varie pagine della mia app iOS?a) Navigatorb) Arrowc) Transitionerd) Segues

Page 50: Xamarin.iOS introduction

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;

}

Page 51: Xamarin.iOS introduction

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);}

Page 52: Xamarin.iOS introduction

Advanced segues

DEMO

Page 53: Xamarin.iOS introduction

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

Page 54: Xamarin.iOS introduction

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();

Page 55: Xamarin.iOS introduction

Alerts

If you target an iOS 8+ device, you can use:• UIAlertController to display an alert• UIActionSheet to display an action sheet

UIAlertController UIActionSheet

Page 56: Xamarin.iOS introduction

Alerts

DEMO

Page 57: Xamarin.iOS introduction

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);}

Page 58: Xamarin.iOS introduction

Managing lists

Page 59: Xamarin.iOS introduction

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

Page 60: Xamarin.iOS introduction

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

Page 61: Xamarin.iOS introduction

Table layout

• Four different built-in styles• To implement a custom layout, you need to

implement a custom cell

Page 62: Xamarin.iOS introduction

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) {

... }}

Page 63: Xamarin.iOS introduction

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;}

Page 64: Xamarin.iOS introduction

Cell reuse

Starting from iO6, there’s a new method that automatically takes care of generating the proper cell for youpublic override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath){ var cell = (MyCell)tableView.DequeueReusableCell(MyCellId, indexPath);

return cell;}

Page 65: Xamarin.iOS introduction

Additional supported features

Page 66: Xamarin.iOS introduction

Managinglists

DEMO

Page 67: Xamarin.iOS introduction

La prima appXamarin.Forms

Page 68: Xamarin.iOS introduction

Domanda 1

Di quali metodi e controlli mi posso avvalere per creare una lista in un’app iOS?

a) UITableViewb) UITableViewContainerc) UITableViewSourced) UITableViewCell

Page 69: Xamarin.iOS introduction

Domanda 1

Di quali metodi e controlli mi posso avvalere per creare una lista in un’app iOS?

a) UITableViewb) UITableViewContainerc) UITableViewSourced) UITableViewCell

Page 70: Xamarin.iOS introduction

Domanda 2

Per implementare un Custom Layout, devo implementare una Custom Cell

a) Verob) Falso

Page 71: Xamarin.iOS introduction

Domanda 2

Per implementare un Custom Layout, devo implementare una Custom Cell

a) Verob) Falso

Page 72: Xamarin.iOS introduction

Domanda 3

Quante tipologie di Layout di default sono presenti?a) Dueb) Quattroc) Ottod) Dodici

Page 73: Xamarin.iOS introduction

Domanda 3

Quante tipologie di Layout di default sono presenti?a) Dueb) Quattroc) Ottod) Dodici

Page 74: Xamarin.iOS introduction

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

Page 75: Xamarin.iOS introduction

Using a custom layout

In the UITableViewSource, you use the custom cell class instead of the base UITableViewCell one in the GetCell() methodpublic 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;}

Page 76: Xamarin.iOS introduction

Custom cells

DEMO

Page 77: Xamarin.iOS introduction

Working with files

Page 78: Xamarin.iOS introduction

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

Page 79: Xamarin.iOS introduction

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

Page 80: Xamarin.iOS introduction

Working withfiles and folders

DEMO

Page 81: Xamarin.iOS introduction

Grazie per l’attenzione

Guido MagrinXamarin Student Partner

@GuidoMagrin