Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

beginning ios programming for dummies
Nội dung xem thử
Mô tả chi tiết
Rajiv Ramnath, PhD is Associate Professor of Practice in computer
science and engineering and Director of Practice at the Collaborative
for Enterprise Transformation and Innovation at The Ohio State
University. Before that, he worked in industry, leading research and
commercial product development .
Cover Image: ©iStockphoto.com/iLexx
Go to Dummies.com®
for videos, step-by-step Photos,
how-to articles, or to shop!
$29.99 USA / $35.99 CAN / £21.99 UK
9 781118 799277
52999
ISBN:978-1-118-79927-7
Open the book and find:
• The basics of object-oriented
programming
• How to use patterns and
frameworks
• Details on setting up the Xcode
Development Environment
• Examples to help you do
object-oriented iOS app design
• How to make your apps fast
and responsive
• Information on designing
user-friendly apps
• How to publish to the App Store
• Ten developer resources for iOS
Programming Languages/iOS
Get coding today with this useful
guide to creating iOS apps
Apple’s iOS is everywhere, thanks to the iPhone, iPad, and other
devices. If you’ve always wanted to be an iOS programming
rock star, this great resource is the guide for you. Discover iOS
programming, including how to get your apps into Apple’s
famous App Store and the best way to make applications that
users will be clamoring to download.
• iOS is everywhere — get started with creating apps forthis highly
popular mobile operating system
• Meeting the methods and platforms — take your object-oriented
programming to the next level with the Xcode Development
Environment
• Making youriOS apps great — make user-friendly apps that are
fast, responsive, complete,robust,reliable, and secure
• Connecting your apps — publish your apps in the App Store
and become an iOS programming star
• Study up — use a long list ofresources to find out more about
object-oriented and iOS programming
Beginning iOS Programming
Ramnath
Rajiv Ramnath, PhD
Author of Android 3 SDK Programming
For Dummies
Learn to:
• Program in the iOS environment using
Xcode® and Cocoa® frameworks
• Create simple interfaces
• Work with libraries, storyboards,
arrays, structures, functions,
and pointers
• Use simulators for testing and
development
Beginning
iOS Programming
Making Everything Easier!™
Start with FREE Cheat Sheets
Cheat Sheets include
• Checklists
• Charts
• Common Instructions
• And Other Good Stuff!
Get Smart at Dummies.com
Dummies.com makes your life easier with 1,000s
of answers on everything from removing wallpaper
to using the latest version of Windows.
Check out our
• Videos
• Illustrated Articles
• Step-by-Step Instructions
Plus, each month you can win valuable prizes by entering
our Dummies.com sweepstakes. *
Want a weekly dose of Dummies? Sign up for Newsletters on
• Digital Photography
• Microsoft Windows & Office
• Personal Finance & Investing
• Health & Wellness
• Computing, iPods & Cell Phones
• eBay
• Internet
• Food, Home & Garden
Find out “HOW” at Dummies.com
*Sweepstakes not currently available in all countries; visit Dummies.com for official rules.
Get More and Do More at Dummies.com®
To access the Cheat Sheet created specifically for this book, go to
www.dummies.com/cheatsheet/beginningiosprogramming
by Rajiv Ramnath and
Cheyney Loffing
Beginning iOS
Programming
Beginning iOS Programming For Dummies®
Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com
Copyright © 2014 by John Wiley & Sons, Inc., Hoboken, New Jersey
Media and software compilation copyright © 2014 by John Wiley & Sons, Inc. All rights reserved.
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted
under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of
the Publisher. Requests to the Publisher for permission should be addressed to the Permissions Department,
John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at
http://www.wiley.com/go/permissions.
Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and
related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be
used without written permission. All other trademarks are the property of their respective owners. John
Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO
REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE
CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT
LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE
CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES
CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE
UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR
OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF
A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE
AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION
OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF
FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE
INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY
MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK
MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN
IT IS READ.
For general information on our other products and services, please contact our Customer Care
Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For
technical support, please visit www.wiley.com/techsupport.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included
with standard print versions of this book may not be included in e-books or in print-on-demand. If this book
refers to media such as a CD or DVD that is not included in the version you purchased, you may download
this material at http://booksupport.wiley.com. For more information about Wiley products, visit
www.wiley.com.
Library of Congress Control Number: 2013954213
ISBN 978-1-118-79927--7 (pbk); ISBN 978-1-118-79931-4 (ebk); ISBN 978-1-118-79932-1 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
Contents at a Glance
Introduction ................................................................ 1
Part I: Getting Started with iOS Programming................ 5
Chapter 1: Entering Mobile Application Development .................................................7
Chapter 2: Object-Oriented Design Principles .............................................................25
Chapter 3: Effectively Using Objective-C......................................................................55
Part II: Meeting the Methods and Platforms................. 89
Chapter 4: Patterns and Frameworks............................................................................91
Chapter 5: Setting Up to Develop iOS Apps................................................................113
Chapter 6: Developing an App on iOS .........................................................................135
Chapter 7: Illustrating Object-Oriented iOS App Design...........................................173
Part III: Making Your iOS Apps Fit for Consumption... 207
Chapter 8: Effectively Using Xcode..............................................................................209
Chapter 9: Developing Your App’s User Interface.....................................................249
Chapter 10: Making Applications Fast and Responsive............................................277
Chapter 11: Making Your App Reliable and Secure...................................................293
Part IV: Connecting Your Apps.................................. 327
Chapter 12: Channeling the World Into Your Device ................................................329
Chapter 13: Harnessing iOS Device Capabilities........................................................345
Chapter 14: Publishing to the App Store ....................................................................375
Part V: The Part of Tens ........................................... 399
Chapter 15: Ten Resources for Object-Oriented Development ...............................401
Chapter 16: Ten Developer Resources for iOS...........................................................413
Index ..................................................................... 423
Table of Contents
Introduction ................................................................. 1
About This Book ..............................................................................................1
Foolish Assumptions.......................................................................................3
Icons Used in This Book .................................................................................3
Beyond the Book .............................................................................................4
Part I: Getting Started with iOS Programming ................ 5
Chapter 1: Entering Mobile Application Development . . . . . . . . . . . . . . 7
Apps for a Mobile Platform ............................................................................7
iOS Benefits ....................................................................................................10
iOS App Development Essentials.................................................................12
Devices ..................................................................................................12
Application development technologies ............................................13
Xcode.....................................................................................................18
The Application Model .................................................................................19
Understanding the lifecycle of an iOS app........................................20
Understanding the structure of an iOS app......................................22
Object-Orientation Concepts .......................................................................23
Chapter 2: Object-Oriented Design Principles . . . . . . . . . . . . . . . . . . . 25
Basic Object-Oriented Concepts .................................................................25
Designing programs.............................................................................26
Structural and behavioral concepts in OO.......................................36
Principles of Object-Orientation..................................................................38
Coupling ................................................................................................39
Cohesion ...............................................................................................40
Designing an Object-Oriented Application.................................................40
Advanced OO Techniques............................................................................42
Delayed binding ...................................................................................42
Delegation.............................................................................................43
Design patterns ....................................................................................43
Inversion of control and software frameworks................................44
The Application Development Lifecycle.....................................................45
Software development lifecycle processes ......................................46
The phases and stages of a software development
lifecycle (SDLC) ................................................................................47
The guiding principles of SDLC methodologies...............................50
Customizing an SDLC methodology ..................................................51
vi Beginning iOS Programming For Dummies
Chapter 3: Effectively Using Objective-C . . . . . . . . . . . . . . . . . . . . . . . . 55
Examining an Objective-C Program.............................................................55
Defining Classes.............................................................................................58
Declaring instance variables ..............................................................61
Declaring methods...............................................................................62
Using forward references....................................................................64
Implementing classes ..........................................................................65
Using Classes, Objects, Methods, and Variables.......................................66
Invoking methods ................................................................................66
Creating objects ...................................................................................67
Using all the language features ..........................................................69
Using introspection and dynamic invocation ..................................80
Managing Memory.........................................................................................82
Handling Exceptions .....................................................................................84
Organizing Program Files .............................................................................85
Analyzing Objective-C’s Object-Orientation Capabilities.........................86
Part II: Meeting the Methods and Platforms ................. 89
Chapter 4: Patterns and Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Common Patterns of OO Design..................................................................91
Seeing basic design patterns..............................................................92
Understanding the Model-View-Controller.......................................98
Making model objects persistent.......................................................99
Avoiding anti-patterns.......................................................................105
Applying patterns to application design.........................................106
Understanding Frameworks .......................................................................106
Inversion of control ...........................................................................106
Basic concepts ...................................................................................107
Flow of control ...................................................................................109
Chapter 5: Setting Up to Develop iOS Apps . . . . . . . . . . . . . . . . . . . . . 113
Becoming an iOS Developer .......................................................................113
Installing, Verifying, and Understanding the Xcode
Development Environment.....................................................................119
Installing Xcode..................................................................................119
Verifying the development environment........................................121
Understanding the elements of an Xcode workspace...................127
Importing and Running the Tic-Tac-Toe Sample Program.....................128
Importing Tic-Tac-Toe and running it on the simulator ...............128
Deploying an App on an iOS device.................................................130
Table of Contents vii
Chapter 6: Developing an App on iOS . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Dissecting an iOS App.................................................................................135
Understanding an App’s Lifecycle.............................................................138
Interacting with the App Through Its User Interface..............................141
Dealing with Data.........................................................................................141
Using JSON..........................................................................................142
Saving data using files .......................................................................145
Moving up to Core Data ....................................................................149
Accessing the Address Book ............................................................158
Setting options for your app.............................................................160
Sharing data across apps..................................................................164
Using Other iOS Capabilities on a Device.................................................165
Visiting the App Templates in Xcode........................................................166
Deconstructing the iOS Framework Using OO Principles
and Design Patterns.................................................................................169
Seeing object-oriented concepts and techniques in iOS ..............169
Seeing patterns in iOS .......................................................................170
Critiquing the iOS framework from the perspective of
OO principles..................................................................................171
Chapter 7: Illustrating Object-Oriented iOS App Design . . . . . . . . . . 173
Customizing an SDLC for iOS App Development.....................................173
Developing Use Cases .................................................................................175
Creating the User Interface ........................................................................177
Illustrating Object-Oriented Design...........................................................180
Classes and responsibilities .............................................................181
Collaborators and missing classes and responsibilities...............183
Contracts and signatures..................................................................186
Implementing an Object-Oriented Design on iOS....................................188
Implementing the model...................................................................188
Creating storyboards and views......................................................193
Making the app active .......................................................................195
Analyzing the OO and Design Principles Used in Tic-Tac-Toe ..............199
Use of design patterns in Tic-Tac-Toe.............................................201
Other concepts...................................................................................204
Part III: Making Your iOS Apps Fit for Consumption.... 207
Chapter 8: Effectively Using Xcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Xcode and IOS — A Beautiful Friendship .................................................210
Setting up your app’s project...........................................................211
Creating your app’s screen flow ......................................................214
viii Beginning iOS Programming For Dummies
Writing your app’s code....................................................................222
Managing and maintaining your app’s components .....................227
Fine-Tuning Your App in Xcode.................................................................237
Configuring your app.........................................................................237
Digging into the Simulator ................................................................239
Exploring your iOS device from Xcode ...........................................240
Debugging your app ..........................................................................243
Using instruments to collect data....................................................244
Getting Help..................................................................................................247
Chapter 9: Developing Your App’s User Interface . . . . . . . . . . . . . . . . 249
Understanding How User Interfaces Work in iOS....................................249
Comprehending event-driven programming in iOS.......................250
Understanding the Application and the Application
Delegate objects in UI processing................................................250
Revisiting the Model-View-Controller Design Pattern ............................253
Understanding the UI Components Available in iOS...............................256
Interacting with buttons, text fields, and labels ............................258
Alerting the user ................................................................................262
Selecting items using pickers ...........................................................264
Showing columnar data using a Table view ...................................266
Going through the other views in the iOS framework...................268
Navigating through a stack of screens using
a Navigation controller..................................................................269
Drawing using 2-D Graphics .............................................................272
Chapter 10: Making Applications Fast and Responsive . . . . . . . . . . . 277
Becoming Familiar with Nonfunctional Requirements ...........................278
Designing Your App at Multiple Levels.....................................................279
Optimizing an Application’s Performance ...............................................280
Using Instrumentation for Code Optimization.........................................282
Ensuring Responsiveness in Your Apps ...................................................287
Ensuring responsiveness by using threading ................................288
Other Performance Considerations for iOS Apps ...................................291
Chapter 11: Making Your App Reliable and Secure . . . . . . . . . . . . . . 293
Making Your App Reliable Through Testing............................................293
Understanding the fundamentals and levels of testing ................294
Understanding the levels of testing.................................................294
Using automated testing ...................................................................297
Adapting to Changes in the App’s Environment .....................................304
Implementing reliability while navigating your app’s lifecycle......304
Dealing with missing or transient capabilities...............................309
Considering Security in an iOS App ..........................................................310
Recognizing the importance of security.........................................310
Table of Contents ix
Looking at security holistically........................................................312
Understanding the attack surface and developing
a threat model for an app..............................................................314
Implementing authentication and access control .........................316
Protecting core data files with encryption.....................................319
Adding auditing to your application ...............................................323
Part IV: Connecting Your Apps .................................. 327
Chapter 12: Channeling the World Into Your Device . . . . . . . . . . . . . 329
Networking Basics .......................................................................................329
Launching a browser from your app...............................................330
Embedding a browser in your iOS application
using WebView...................................................................................332
Using Maps and Providing Location-Based Services ..............................334
Installing the additional development components needed
for maps...........................................................................................336
Displaying a map using a MapView .................................................337
Using geocoding to navigate the map .............................................338
Wherever you go, there you are ......................................................339
Design Principles for Web- and Location-Based Apps............................343
Being nice about using remote services .........................................343
Using threading to deal with slow network connections .............343
Chapter 13: Harnessing iOS Device Capabilities . . . . . . . . . . . . . . . . 345
Integrating E-Mail, SMS, and the Phone into Your App ..........................346
Sending e-mail from your app ..........................................................346
Sending an SMS from your app ........................................................349
Placing telephone calls from your app ...........................................351
Playing and Capturing Audio, Video, and Images ...................................352
Playing and recording video.............................................................353
Playing and recording audio ............................................................356
Working with images .........................................................................358
Seeing the Capabilities Available on Your Device...................................361
Testing for network availability.......................................................361
Testing for sound and camera capabilities ....................................362
Declaring an app’s needs in the .plist file.......................................364
Bringing in the Outside World with Sensors............................................364
Sensing orientation............................................................................365
Sensing the accelerometer, gyroscope, and magnetometer ........367
Examining OO and Other Design Principles in this Chapter..................370
Seeing uses of loose coupling...........................................................371
Using delegation for customized processing .................................371
Using design patterns........................................................................372
Design tradeoffs in Tic-Tac-Toe .......................................................373
x Beginning iOS Programming For Dummies
Chapter 14: Publishing to the App Store . . . . . . . . . . . . . . . . . . . . . . . . 375
Creating Your Developer Account ............................................................375
Distributing Applications ...........................................................................378
Providing basic application details .................................................378
Special capabilities ............................................................................381
Internationalization and localization...............................................382
Testing ..........................................................................................................383
iOS devices .........................................................................................383
Beta testing.........................................................................................388
Submitting Your App...................................................................................390
Using iTunes Connect........................................................................390
Uploading to the App Store ..............................................................394
Supporting Your App ..................................................................................396
Marketing ............................................................................................396
Sales data and customer feedback ..................................................397
Publishing updates ............................................................................398
Part V: The Part of Tens............................................ 399
Chapter 15: Ten Resources for Object-Oriented Development . . . . . 401
Design Patterns: Elements of Reusable Object-Oriented
Software...........................................................................................401
martinfowler.com...............................................................................402
Object-Oriented Programming with Objective-C...........................403
The Journal of Object Technology ..................................................404
Massively Open Online Courses (MOOCs).....................................405
Ambysoft.............................................................................................406
Craig Larman’s Use Case Primer......................................................407
uml.org ................................................................................................408
Agile Alliance......................................................................................409
Rajiv’s YouTube Video Series ..........................................................410
Chapter 16: Ten Developer Resources for iOS . . . . . . . . . . . . . . . . . . . 413
iOS Dev Center ...................................................................................413
Apple Worldwide Developers Conference......................................414
iOS Dev Weekly ..................................................................................415
raywenderlich ....................................................................................416
AppCoda..............................................................................................417
Stack Overflow ...................................................................................418
iPhoneDevSDK....................................................................................419
Table of Contents xi
pttrns...................................................................................................420
Cocoa Controls...................................................................................420
MacRumors.........................................................................................421
Index....................................................................... 423
xii Beginning iOS Programming For Dummies
Introduction
Although iOS powers only about 13 percent of the smartphones used by
people in the U.S., its use increases to 55 percent when you consider traffic
over the Internet — because iOS devices are bought by people who use their
devices frequently. In addition to its loyal users, iOS comes with a well-designed
Objective-C–based SDK with rich functionality that makes developing apps
straightforward and fun. In other words, iOS is a perfect target market for an
innovative app developer. With that, welcome to this book.
About This Book
This book guides you through the iOS SDK and how to build high-quality applications using it. It focuses on iOS 7 and Xcode version 5 and is of significant
value to software developers, regardless of their level of experience.
✓ If you’re a software developer and want to understand how to apply
object-oriented concepts, techniques, and principles to iOS development, this book is for you.
✓ If you’re a software developer and have developed other kinds applications
but not those for mobile devices, don’t worry. This book is a mobile applications primer that deals with resource conservation, network disconnection,
change in location, hardware-software interaction, and more.
✓ If you’re a software developer with experience in developing mobile
applications and want to develop an equivalent iOS application (such as
iPhone), you’ll probably quickly understand the iOS programming model
and then navigate on to the chapters you’re most interested in.
Here are some of the conventions you will see in the book:
✓ Code examples: These appear in a fixed-width font so that they stand
out, as illustrated here:
[self initializeGameSession];