Documente Academic
Documente Profesional
Documente Cultură
ix
table of contents
getting started
Going mobile
The iPhone changed everything.
Its a gaming platform, a personal organizer, a full web browser, oh yeah, and a phone. The iPhone is one of the most exciting devices to come out in some time, and with the opening of the App Store, its an opportunity for independent developers to compete worldwide with big named software companies. All you need to release your own app are a couple of software tools, some knowledge, and enthusiasm. Apple provides the software and well help you the knowledge; were sure youve got the enthusiasm covered. Theres a lot of buzz and a lot of money tied up in the App Store... Mobile applications arent just ported desktop apps Anatomy of an iPhone app Mike cant make a decision Make a good first impression It all starts with the iPhone SDK
What should I do?
2 3 5 6 7 8 10 12 13 14 16 17 22 23 24 24 25 35
Xcode includes app templates to help you get started Xcode is the hub of your iPhone project... ...and plays a role in every part of writing your app Build your interface using... Interface Builder Add the button to your view The iPhone Simulator lets you test your app on your Mac What happened? Use Interface Builder to connect UI controls to code Interface Builder lists which events a component can trigger Elements dispatch events when things happen to them Connect your events to methods Your iPhone Toolbox
table of contents
First we need to figure out what Mike (really) wants App design rulesthe iPhone HIG HIG guidelines for pickers and buttons Create a new View-based project for Insta-Twit The life of a root view First, get the data from Mike Use pickers when you want controlled input Fill the picker rows with Mikes data Pickers get their data from a datasource... Theres a pattern for that First, declare that the controller conforms to both protocols The datasource protocol has two required methods Connect the datasource just like actions and outlets Theres just one method for the delegate protocol The button needs to be connected to an event Connect the picker to our outlet Use our picker reference to pull the selected values Your iPhone Toolbox
39 44 47 48 52 55 56 57 58 59 64 66 67 68 72 78 79 87
xi
table of contents
Renee is catching on.... Make room for custom input Header files describe the interface to your class Auto-generated accessors also handle memory management To keep your memory straight, you need to remember just two things But when Mikes finished typing... Customize your UITextField Components that use the keyboard ask it to appear... Ask the textField to give up focus Messages in Objective-C use named arguments Use message passing to tell our view controller when the Done button is pressed Somethings still not right
xii
table of contents
4
night. Is there an app for that?
multiple views
A table with a view
Most iPhone apps have more than one view.
Weve written a cool app with one view, but anyone whos used an iPhone knows that most apps arent like that. Some of the more impressive iPhone apps out there do a great job of moving through complex information by using multiple views. Were going to start with navigation controllers and table views, like the kind you see in your Mail and Contact apps. Only were going to do it with a twist...
Look, I dont have time for posting to Twitter. I need to know a ton of drink recipes every
So, how do these views fit together? The navigation template pulls multiple views together The navigation template starts with a table view A table is a collection of cells Just a few more drinks Plists are an easy way to save and load data Arrays (and more) have built-in support for plists Use a detail view to drill down into data A closer look at the detail view Use the navigation controller to switch between views Navigation controllers maintain a stack of views Dictionaries store information as key-value pairs Debuggingthe dark side of iPhone development First stop on your debugging adventure: the console Interact with your application while its running Xcode supports you after your app breaks, too The Xcode debugger shows you the state of your application What the heck is going on? Your iPhone Toolbox
135 136 137 140 148 150 153 156 157 167 168 172 175 176 177 178 179 181 183
xiii
table of contents
5
Anatomy of a crash
Dictionary name = Cupids Cocktail ingredients = Cherry liqueur, peach ... directions = Shake
It all started with Sam... Use the debugger to investigate the crash Update your code to handle a plist of dictionaries The detail view needs data Each dictionary has all the information we need We have a usability problem Use a disclosure indicator if your cell leads to more information Sales were going strong... Use navigation controller buttons for editing The button should create a new view We need a view... but not necessarily a new view The view controller defines the behavior for the view A nib file contains the UI components and connections... You can subclass and extend views like any other class Modal views focus the user on the task at hand... Any view can present a modal view Our view doesnt have a navigation bar Create the save and cancel buttons Write the save and cancel actions Your iPhone Toolbox
186 188 191 194 195 201 203 206 211 215 216 217 218 219 224 225 230 232 233 237
xiv
table of contents
NSNotification object
xv
table of contents
HF bounty hunting Choose a template to start iBountyHunter Drawing how iBountyHunter works... Build the fugitive list view Next up: the captured view After a quick meeting with Bob... Core Data lets you focus on your app Core Data needs to know what to load Core Data describes entities with a Managed Object Model Build your Fugitive entity Whip up a Fugitive class without writing a line Use an NSFetchRequest to describe your search Add the database as a resource The template sets things up for a SQLite DB The iPhones application structure defines where you can read and write Copy the database to the correct place To be continued... Your Core Data Toolbox
304 308 310 316 318 327 329 330 333 334 341 344 354 355 358 359 373 375
Fugitive
xvi
table of contents
8
captured - Boolean
Bob needs documentation Everything stems from our object model The data hasnt been updated Data migration is a common problem We need to migrate the old data into the new model Xcode makes it easy to version the data model Core Data can lightly migrate data Bob has some design input A quick demo with Bob Use predicates for filtering data We need to set a predicate on our NSFetchRequest Core Data controller classes provide efficient results handling Time for some high-efficiency streamlining Next we need to change the search to use the controller... Refactor viewWillAppear to use the controller We need to refresh the data Your Data Toolbox
378 381 384 385 386 387 389 394 406 408 409 416 417 417 418 423 429
xvii
table of contents
For Bob, payment requires proof ! The way to the camera... Theres a method for checking Prompt the user with action sheets Bob needs the where, in addition to the when Core Location can find you in a few ways Add a new framework Just latitude and longitude wont work for Bob Map Kit is new with iPhone 3.0 A little custom setup for the map Annotations require a little more finesse Your extras Toolbox Its been great having you here!
432 441 451 452 458 464 466 472 473 474 479 485 486
xviii
table of contents
appendix i, leftovers
The top 6 things (we didnt cover)
Ever feel like somethings missing? We know what you mean... Just when you thought you were done, theres more.
We couldnt leave you without a few extra details, things we just couldnt fit into the rest of the book. At least, not if you want to be able to carry this book around without a metallic case and castor wheels on the bottom. So take a peek and see what you (still) might be missing out on.
#1. Internationalization and Localization Localizing string resources #2. UIWebView #3. Device orientation and view rotation Handling view rotations Handling rotation with two different views #4. View animations #5. Accelerometer Understanding the device acceleration #6. A word or two about gaming... Quartz and OpenGL
488 490 492 494 495 496 497 498 499 500 501
xix
table of contents
Apple has rules The Provisioning Profile pulls it all together Keep track in the Organizer
xx