iOS App Components - Mobile Application Development · iOS App Components CS 4720 – Mobile...

25
CS 4720 iOS App Components CS 4720 – Mobile Application Development

Transcript of iOS App Components - Mobile Application Development · iOS App Components CS 4720 – Mobile...

CS4720

iOSAppComponents

CS4720– MobileApplicationDevelopment

CS4720

iOSArchitecture

2

CS4720

BuildingBlocks• UIApplication

– Themainentrypointforyourapp– Eachapphasexactlyoneinstanceofthisclass– ProvidesthemaininterfacebacktotheOS– HandlesallincominginfofromtheOS(suchastouchevent,memorywarnings,incomingphonecall,etc.)

– Passesthesemessagesoffto…

3

CS4720

BuildingBlocks• UIApplicationDelegate

– Managestherunningofyourapp– Handles“major”events,likeappswtiching,appinitialization,etc.

4

CS4720

AppDelegate.swift

5

import UIKit

@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

CS4720

AppDelegate.swift

6

func application(_ application: UIApplication, didFinishLaunchingWithOptionslaunchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {return true

}

CS4720

OtherComponents• UIDocument: allowsforinternalappdocumentsanddatastores

• ViewController:managesallviews(scenes)fortheapp

• UIWindow:theonewindowoftheiOSdevice(onlyhavemoreifexternaldisplay)

• ViewObjects:allthewidgetsinascene

7

CS4720

TheMainAppLoop

8

CS4720

TheMainAppLoop• TheOSreceivesinputandpassesittotheUIApplication

• WhichpassesittotheUIApplicationDelegate

• WhichpassesittotheUIWindow• WhichpassesittothecurrentlyseenViewController

9

CS4720

TheMainAppLoop• Othereventsarepassedtothe“FirstResponder”objectthatisavailable

• Everythingthatcanacceptandrespondtoaneventisaresponderobject

• Thefirstresponderobjectisthetop-leveldesignatedobjectinaviewtohandleevents

10

CS4720

AppStates

11

CS4720

AppStates• applicationWillEnterForeground – startingtomovetoactivestate

• applicationDidBecomeActive – calledrightbeforeviewdisplayed

• applicationWillResignActive – firstcallbeforegoingintobackground

• applicationDidEnterBackground – nowisinbackground

• applicatinoWillTerminate – willend

12

CS4720

OnLaunch• application comesinwithdidFinishLaunchingWithOptions

• CheckdictionarylaunchOptions forinfoonwhywaslaunched(somewhatlikelookingattheIntent)

• Anyappnotrespondingin5secondsiskilled• Startinitialization• UIKit grabsfirststoryboardandViewController

13

CS4720

InsidetheView

14

CS4720

UsingSegues• InsteadofIntentslikeAndroid,we’lluseSeguestopassdatabetweenScenes/ViewControllers

15

CS4720

MVCiniOS• WeagainseeModel-View-Controlleraspartofthefoundationforamobilesystem

• BecauseofthenatureofObjective-CandNeXTSTEP,MVCisoneoftheprimarydesignpatternsforbothiOSandOSX

16

CS4720

MVCiniOS

17

CS4720

MVCiniOS

18

CS4720

MVCiniOS• Model:Thebaseclassesyouwritetoholddata

– CouldsubclassNSObject/Object– Couldconnecttoadatabaseorotherdatasource– Couldbeasimpleclassyouwrite

19

CS4720

MVCiniOS• View:Anyrectangulardrawable objectonthescreen– VariousStack,Table,andCollectionViews– Image,Text,PickerViews– MapandWebKit View– SceneKitView(for3Dscenes)– Managesdrawingitsareaonthescreen– Cancontainotherviews(orbecontained)– Respondstotouchandotherevents

20

CS4720

MVCiniOS• Controller:TheViewController class

– EachViewController managesahierarchyofViews– Theview propertyoftheclasscontainstheroot– Viewsareaccesslazily;thatis,theyareonlyloadedwhenneeded

– Updatesthecontentsoftheviews,usuallyinresponsetochangestotheunderlyingdata

– Respondstouserinteractionswithviews– Resizesviewsandmanagesthelayoutoftheoverallinterface

21

CS4720

BuildingupinMVC• Startbyconsideringyourdata

– Wheredoesitcomefrom?– Howwillyoustoreit?– Whatsortofaccessdoyouneedfromit?

22

CS4720

BuildingupinMVC• Storyboardyouridea

– JustaswithAndroid,layouteachscreen– Whatviewsmakeupeachscreen?

• Text?• ATable?• AnImage?

– Whathappenswhenyoutouchorswipeoneachview?Oronthescreen?

23

CS4720

BuildingupinMVC• CreatetheappropriateControllertype• AddViewstotheControllertogetthelayoutthewayyouwantit

• Startwithdummydata• Runinthesimulatoroften!• Checkandadjustyourconstraintstogeteverythingonscreen

• Testrotation!

24

CS4720

BuildingupinMVC• InyourStoryboard,linkthevariousViewsbacktothecodeusingctrl-click/drag

• Dothesameforbuttonsandothercontrols• Loaddataasneededtorefreshtheview(oftenhappensautomatically)

• Addincodetohandleuserevents,liketouchesandswipes

• AddNavigationControllerstoallowforswitchingbetweenscenes

25