Developing Ios Apps Developing Ios Apps Today
Total Page:16
File Type:pdf, Size:1020Kb
Start Developing iOS Apps TTodayoday Contents Introduction 5 Setup 6 Get the Tools 7 Tutorial: Basics 8 Create a New Project 9 Get Familiar with Xcode 11 Run iOS Simulator 12 Review the Source Code 14 Create a Storyboard 17 Add a Scene to Your Storyboard 19 Test Your Changes 23 Build the Basic Interface 23 Recap 28 Structuring an App 29 App Development Process 30 Defining the Concept 30 Designing a User Interface 31 Defining the Interaction 31 Implementing the Behavior 32 Objects Are Building Blocks for Apps 32 Classes Are Blueprints for Objects 32 Objects Communicate Through Messages 33 Protocols Define Messaging Contracts 34 Incorporating the Data 34 Use the Right Resources 34 Incorporate Real Data 35 Designing a User Interface 36 The View Hierarchy 36 Building an Interface Using Views 37 2012013-10-223-10-22 | CopyrigCopyrightht © 20120133 Apple Inc. All RightRightss ReserReserved.ved. 22 Contents Use Storyboards to Lay Out Views 38 Use Inspectors to Configure Views 40 Use Auto Layout to Position Views 41 Defining the Interaction 42 View Controllers 42 Actions 43 Outlets 44 Controls 44 Navigation Controllers 45 Use Storyboards to Define Navigation 45 Tutorial: Storyboards 48 Adopt Auto Layout 49 Creating a Second Scene 51 Display Static Content in a Table View 54 Add a Segue to Navigate Forward 56 Create Custom View Controllers 64 Unwind a Segue to Navigate Back 67 Recap 70 Implementing an App 71 Incorporating the Data 72 Designing Your Model 72 Implementing Your Model 73 Using Design Patterns 74 MVC 74 Target-Action 75 Delegation 75 Working with Foundation 77 Value Objects 77 Strings 78 Numbers 79 Collection Objects 80 Arrays 80 Sets 84 Dictionaries 85 2012013-10-223-10-22 | CopyrigCopyrightht © 20120133 Apple Inc. All RightRightss ReserReserved.ved. 33 Contents Represent nil with NSNull 86 Writing a Custom Class 88 Declaring and Implementing a Class 89 Interface 89 Implementation 89 Properties Store an Object’s Data 90 Methods Define an Object’s Behavior 91 Method Parameters 92 Implementing Methods 93 Tutorial: Add Data 95 Create a Data Class 96 Load the Data 97 Display the Data 100 Mark Items as Completed 104 Add New Items 107 Recap 113 Next Steps 114 iOS Technologies 115 User Interface 115 Games 116 Data 117 Media 117 Finding Information 118 Use Contextual Help Articles for Xcode Guidance 118 Use Guides for General and Conceptual Overviews 120 Use API Reference for Class Information 121 Use Quick Help for Contextual Source Code Information 126 Use Sample Code to See Real-World Usage 128 Where to Go from Here 130 Taking the ToDoList App to the Next Level 131 Document Revision History 132 2012013-10-223-10-22 | CopyrigCopyrightht © 20120133 Apple Inc. All RightRightss ReserReserved.ved. 44 Introduction ● “Setup” (page 6) ● “Tutorial: Basics” (page 8) 2200113-10-223-10-22 | CopyrigCopyrighhtt ©© 22001133 Apple Inc. AAllll RightRightss ReserReserved.ved. 55 Setup Start Developing iOS Apps Today provides the perfect starting point for iOS development. On your Mac, you can create iOS apps that run on iPad, iPhone, and iPod touch. View this guide’s four short modules as a gentle inintrtroduoductictionontoto bubuilildindinggyoyourur fifirstrstapapp—p—ininclcludiudingng ththeetotoololssyoyouuneneededananddththeemamajorjorcoconcncepeptsts ananddbebestst prpractacticiceses that will ease your path. The first three modules each end with a tutorial, where you’ll implement what you’ve learned. At the end of the last tutorial, you’ll have created a simple to-do list app. After you’ve built your first app in this guide and are considering your next endeavor, read the fourth module. It explores the technologies and frameworks you might consider adopting in your next app. You’ll be on your way to keeping your customers engaged and looking fforwardorward to the next great thing. Even though this guide takes you through every step of building a simple app, to benefit most it helps to be acquainted with computer programming in general and with object-oriented programming in particular. 2012013-10-223-10-22 | CopyrigCopyrightht © 20120133 Apple Inc. All RightRightss ReserReserved.ved. 66 Setup Get the Tools Get the Tools Before you can start developing great apps, set up a development environment to work in and make sure you have the right tools. TToo develop iOS apps, you need: ● A Mac computer running OS X 10.8 (Mountain Lion) or later ● Xcode ● iOS SDK Xcode is Apple’s integrated development environment (IDE). Xcode includes a source editor, a graphical user iinntteerrffaacceeediedittoorr,,aannddmamannyyootthheerrffeeaattureuress..TThheeiiOOSSSSDDKKextextendendsstthheeXXccodeodetotoolsolsetet toto ininclcludeudetthheettoooollss,,ccoompmpiilleerrss,, and frameworks you need specifically for iOS develodevelopment.pment. YYoouuccaannddoownwnllooaaddtthheellaatteessttvveerrssiioonnooffXXccooddeeffoorrffrreeeeffrromom tthheeAAppppSSttoorreeoonnyyoouurrMaMacc..((YYoouunneeeeddOOSSXXvveerrssiioonn 10.8 to download Xcode. If you have an earlier version of OS X, you need to upgrade.) The iOS SDK is included with Xcode. TToo download the latest version of Xcode 1. Open the App Store app on your Mac (by default it’s in the Dock). 2. In the search field in the top-right cornercorner,, type Xcode and press the Return key. 3. CClliicckk FFrreeee .. Xcode is downloaded into your /Applications directory. 2200113-10-223-10-22 | CopyrigCopyrighhtt ©© 22001133 Apple Inc. AAllll RightRightss ReserReserved.ved. 77 Tutorial: Basics This tutorial takes you through the basics of what an app is, to the process of creating a simple use userrinteinterfacerface,, and on to adding the custom behavior that transforms the interface into a working app. Follow this tutorial to learn the basics of iOS app development, including: ● How to use Xcode to create and manage a project ● How to identify the key pieces of an Xcode project ● How to add standard user interface elements to your app ● How to build and run your app After you finish the tutorial, you’ll have an app that looks something like this: TToo keep things simple, the tutorial project has ononlyly an iPhone interface, but you use the exact same tootoolsls and techniques to develop an iPad app. This tutorial uses Xcode 5.0 and iOS SDK 7.0. 2012013-10-223-10-22 | CopyrigCopyrightht © 20120133 Apple Inc. All RightRightss ReserReserved.ved. 88 Tutorial: Basics Create a New Project Create a New Project To get started developing your app, create a new Xcode project. Xcode includes several built-in app templates that you can use to develop common styles of iOS apps, such as games, apps with tab-based navigation, and table-view-based apps. Most of these templates have preconfigurpreconfigureded interface and source code files for you to start working with. For this tutorial, you’ll ststartart with the most basic template: Empty Application. Working with the Empty Application templatemplatete will hehelplp you understand the basic structure of an iOS app and hohowwconcontetentntgetgetssonsonscrecreen.en.AftAfterer yoyou’u’vevelealearnerneddhowhoweveveryterythinhinggworworksks,,yoyouucancanuseuseoneoneofofthetheothotherer temtemplaplatetess for your own app to save yourself some configuration time. To create a new empty project 1. Open Xcode from the /Applications directory. The Xcode welcome window appears. IfIf aaprprojojectectwiwindndowowapappeapearsrs ininststeaeaddofof ththeewewelclcomomeewiwindndowow,,dondon’t’twoworryrry—y—youou prprobobabablyly crcreaeateteddoror opopeneneded a project in Xcode previously. Just use the menu item in the next step to create the project. 2. In the welcome winwindow,dow, cliclickck “Create a new XXcodecode project” (or choose FFileile > New > Project). 2012013-10-223-10-22 | CopyrigCopyrightht © 20120133 Apple Inc. All RightRightss ReserReserved.ved. 99 Tutorial: Basics Create a New Project Xcode opens a new window and displays a dialog in which you can choose a template. 3. In the iOS section at ththee left of the dialog, selselectect ApplicatioApplication.n. 4. In the main area of tthehe dialog, clicclickk Empty ApplicatApplicationion and then clickclick Next. 5. In the dialog that appears, namnamee your app and chochooseose additional optionsoptions for your proproject.ject. Use the following values: ● Product Name: ToDoList 2012013-10-223-10-22 | CopyrigCopyrightht © 20120133 Apple Inc. All RightRightss ReserReserved.ved. 1100 Tutorial: Basics Get Familiar with Xcode Xcode uses the product name you entered to name your project and the app. ● Company Identifier: Your company identifier, if you have one. If you don’t, use com.example.. ● Class Prefix: XYZ Xcode uses the class prefix name to name the classes it creates for you. Objective-C classes must be nnaamemedduunniiqquueellyywwiitthhiinnyyoouurrccooddeeaannddaaccrroossssaannyyffrramamewewoorrkkssoorrbbuunnddlleessyyoouummiigghhttbbeeuussiinngg..TTookkeeeepp class names unique, the convention is to use prefixes for all classes. Two-letter prefixes are reserved by Apple for use in framework classes, so use something that’s three letters or longer. 6. Choose iPiPhonehone frfromom the Devices pop-up menu. AAssaallrreadeadyymenmenttiioonedned,,crcreateatiinnggaannappappwitwithhaanniiPPhonhoneeininteterfacrfaceeisis ththeesimsimplplesesttwawayytoto stastarrtt..TThheetteecchhniqniquueess used are the same for an iPad or universal app. 7. Click Next. 8. In the dialog that appears, choochoosese a location fforor your project and click Create. Xcode opens your new project in a window (called the workspace window), which should look