Xcode® 5

Published by
John Wiley & Sons, Inc.
10475 Crosspoint Blvd.
Indianapolis, IN 46256
www.wiley.com

Copyright © 2014 by John Wiley & Sons, Inc., Indianapolis, Indiana

Published by John Wiley & Sons, Inc., Indianapolis, Indiana

Published simultaneously in Canada

ISBN: 978-1-118-83433-6

Manufactured in the United States of America

10 9 8 7 6 5 4 3 2 1

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 either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. 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.

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 or to obtain technical support, 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.

Library of Congress Control Number: 2013956856

Trademarks: Wiley and the Wiley logo are registered trademarks of John Wiley & Sons, Inc., in the United States and other countries, and may not be used without written permission. Xcode is a registered trademark of Apple, Inc. 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.

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.

For Jan

About the Author

Richard Wentk is one of the U.K.’s most reliable technology writers, with more than ten years of experience as a developer and more than fifteen years in publishing. He covers Apple products and developments for Macworld and MacFormat magazines and writes about technology, creativity, and business strategy for titles such as Computer Arts and Future Music. As a trainer and former professional Apple developer returning now to development on the iPhone and OS X, he is uniquely able to clarify the key points of the development process, explain how to avoid pitfalls and bear traps, and emphasize key benefits and creative possibilities. He lives online but also has a home in Wiltshire, England. For details of apps and other book projects, visit www.zettaboom.com.

Credits

Acquisitions Editor

Aaron Black

Project Editor

Martin V. Minner

Technical Editor

Brad Miller

Copy Editor

Gwenette Gaddis

Director, Content Development & Assembly

Robyn Siesky

Vice President and Executive Group Publisher

Richard Swadley

Acknowledgments

Although book publishing has become digital, book writing hasn't. A book continues to be a team effort.

My thanks go to acquisitions editor Aaron Black for making the project happen and to project editor Martin V. Minner for his continuing support and extended patience. Sincere thanks are also due to the rest of the team at Wiley for their hard work behind the scenes, especially technical editor Brad Miller, whose feedback and comments were invaluable.

Personal gratitude is due to Annette, Alexa, and Hilary, who all contributed more than they know.

Software development has become a communal activity, and particular appreciation is due to the countless bloggers, experimenters, developers, and problem-solvers on the web whose generosity and creativity have made so much possible in so many ways.

Finally, love as always to Team HGA. I couldn’t have written it without you.

Preface

Having become comfortable with Xcode 4, I was curious about the changes that Apple would make in Xcode 5. Some, such as the removal of the old GCC toolchain and its replacement with an all-LLVM compiler and debugger, were expected. Others, such as further simplification of the permission profiling and app submission process, were very welcome.

Xcode 5’s designers have continued to simplify app development and make it more accessible to the public. There is no doubt that the App Store has been a phenomenal success, with millions of contributions from both professional and amateur developers. The latter have proven that Xcode works as intended. Starting with little or no software experience, they have developed and sold apps to an international market. Even if the quality is variable, the sheer number of apps proves that app development isn’t quite the mystery it once was.

As Apple moves into new markets, the number of newcomers continues to grow. So the first goal of this book is to introduce the key features of Xcode 5 for those who are just getting started. Newcomers should begin at the front of the book and work their way through it in order. The sequence of the earlier chapters is designed to be a practical primer for Xcode development, not just a list of features and changes.

A second goal is to highlight important changes, and introduce some of the more complex features in more detail. Many newcomers use Xcode in a simple click-bang way, missing out on the power and flexibility hidden under the surface. The less-obvious features are easy to skip, but exploring them can open up new possibilities for testing, debugging, project management, and build control.

Some tools, such as Instruments, have further hidden layers of their own that would require a further book the size of this one to explore fully. Others, such as bots, are new in Xcode 5. Still others, including details of the build system, have been expanded in this edition, with extra notes about the challenges developers can encounter when trying to integrate code from projects built in other environments.

Because space is limited, this book doesn’t dig into every feature to the deepest possible level. But new Xcode 5 users and experienced Xcode 5 users should find creative inspiration here, as well as a good store of tips and techniques that can push them through the essentials of basic development and into the deeper possibilities of managed testing and build design.

Every author works hard to make his or her books as helpful as possible. Comments and feedback are welcome at xcodedr@zettaboom.com.

Xcode 5 Developer Reference

Table of Contents

About the Author

Preface

Acknowledgments

Introduction

Part 1: Getting Started with Xcode 5

Chapter 1: Introducing Xcode 5

Understanding the History of Mac Development Tools

Looking back at early IDEs

Developing Xcode

Understanding Xcode 5’s Key Features

Moving to Xcode 5

Introducing the Xcode 5 editor

Working with Interface Builder

Exploring code and file management

Exploring the debugger

Comparing iOS and OS X Development

Developing for OS X

Developing for iOS

OS X and iOS cross-development

Summary

Chapter 2: Getting and Installing Xcode

Selecting a Mac for Xcode

Choosing a processor

Selecting memory

Choosing a monitor

Working with disk storage

Choosing peripherals

Choosing supporting software

Getting Started with Development

Registering as a developer

Choosing a program

Accessing developer resources

Getting Started with Xcode

Installing the Public Version

Installing the Developer Version

Installing Xcode

Creating and organizing project folders

Working with multiple versions of Xcode

Uninstalling Xcode

Summary

Chapter 3: Building Applications from Templates

Getting Started with Templates

Building a project from an application template

Looking inside the Application templates

Using the OS X Application templates

Understanding the iOS Application templates

Introducing the Framework and Library templates

Introducing the Plug-In templates

Introducing the Other templates

Customizing Projects and Templates

Summary

Chapter 4: Navigating the Xcode Interface

Understanding the Interface

Using the Navigation Area

The Project Navigator

The Symbol Navigator

The Search Navigator

The Issue Navigator and the Log Navigator

The Breakpoint Navigator and the Debug Navigator

Loading the navigators from menus and keyboard shortcuts

Using the Utility Area

Displaying file information

Using Quick Help

Adding custom comments

Using the library area

Working with the Editor Area

Getting started with code editing

Introducing build management

Summary

Chapter 5: Working with Files, Frameworks, and Classes

Working with Files and Classes

Adding new files to a project

Adding a new class

Adding new resources

Adding miscellaneous other files from the templates

Deleting and renaming files

Renaming projects

Importing existing files

Reimporting missing files

Working with Groups

Moving groups and files

Creating a new group

Organizing groups

Working with Frameworks

Adding Apple frameworks

Using frameworks

Adding other frameworks

Summary

Chapter 6: Using Documentation

Understanding the Apple Documentation

Comparing iOS and OS X and their different versions

Understanding resource types

Using Topics

Using frameworks and layers

Searching the online documentation

Using the Xcode Documentation Viewer

Exploring documentation options

Browsing the documentation

Searching the documentation

Using class references

Accessing documentation from code

Using Other Documentation

Summary

Chapter 7: Getting Started with Interface Builder

Understanding IB

Understanding nib files and storyboards

Understanding controllers and views

Getting Started with IB

Introducing IB for iOS

Introducing IB for OS X

Exploring the utility area

Creating a Simple iOS Project with IB

Understanding UI design

Understanding IB and touch events

Adding objects to a view

Setting object contents

Linking objects to code

Creating a universal app

Understanding the IB Object Hierarchy

Designing without IB

Summary

Chapter 8: Creating More Advanced Effects with Interface Builder

Working with Constraints

Introducing constraints and auto-layout

Understanding constraints

Getting started with constraints

Aligning object edges

Justifying objects with spacer views

Getting Started with Storyboards

Understanding segue types

Creating a modal segue

Adding a view controller subclass

Adding a navigation controller

Using table views

Understanding Placeholders

Using File’s Owner

Using First Responder

Using Exit

Subclassing and Customizing Views

Creating simple subclasses

Working with more advanced subclassing techniques

Creating static image backgrounds

Creating a colored background

Importing an image file

Assigning an image file

Supporting Non-English Languages

Working with Advanced IB Techniques

Using tags

Converting iPhone projects to the iPad

Handling resolution issues

Summary

Part 2: Going Deeper

Chapter 9: Saving Time in Xcode

Using Code Folding

Editing Structure

Refactoring Code

Using the Refactor menu

Using the Edit All in Scope feature

Using Code Snippets

Inserting a snippet

Creating a new snippet

Jumping to a Definition

Revealing Symbols

Summary

Chapter 10: Using the Organizer

Working with the Library

Enabling developer mode

Reviewing and managing profiles

Finding and installing iOS updates

Using device logs

Working with screenshots

Managing individual devices

Working with Projects and Archives

Managing Projects

Creating Archives

Summary

Chapter 11: Managing Certificates, Identifiers, and Provisioning Profiles

Understanding Provisioning

Understanding accounts and team roles

Understanding entitlements, devices, and profiles

Getting Started with Provisioning

Collecting developer resources

Creating Certificates and Identities

Working on more than one Mac

Managing devices

Creating Certificates Manually

Creating a CSR

Summary

Chapter 12: Working with Builds, Schemes, and Workspaces

Getting Started with the Xcode Build System

Creating and organizing a workspace

Working with projects and targets

Understanding settings and options

Working with Schemes and Build Configurations

Getting started with build configurations

Understanding schemes and build actions

Editing schemes

Managing schemes, build actions, and targets

Summary

Chapter 13: Customizing Builds for Development and Distribution

Introducing Common Basic Customizations

Creating a build for testing and debugging

Selecting the Base SDK

Setting the minimum supported OS version

Including a framework or library in a build

Selecting a compiler and C/C++ dialect

Controlling warnings

Disabling iOS PNG compression

Selecting an architecture

Changing an app’s name before shipping

Creating an App Store Submission

Uploading marketing and support materials

Building and packaging an iOS project for the App Store

Creating a Mac App Store submission

Working with iOS Ad Hoc builds

Creating and installing an Ad Hoc provisioning profile

Creating an Ad Hoc build

Distributing an Ad Hoc build

Creating Mac Developer ID Builds

Setting up a Developer ID build

Creating installer packages

Summary

Chapter 14: Advanced Workspace and Build Management

Understanding the Build Process

Creating Custom Builds

Introducing conditional compilation

Using conditional compilation

Working with Build Phases

Setting up build phases

Creating build rules

Customizing command-line builds

Working with Libraries and Headers

Working with workspaces

Linking libraries with apps

Creating an application in a workspace

Summary

Chapter 15: Using Version Control

Using Manual Version Control

Using Snapshots

Working with Git Source Control

Understanding Git

Using Git locally

Using Xcode with GitHub

Summary

Part 3: Creating Fast and Reliable Code

Chapter 16: Getting Started with Code Analysis and Debugging

Checking and Analyzing Code

Checking code as you enter it

Using Fix-It code tips

Using the Static Code Analyzer

Getting Started with Debugging

Using the Console

Creating custom messages

Using custom messages

Understanding crashes

Viewing multiple logs

Working with Breakpoints

Working with simple breakpoints

Using the Variables View

Using advanced breakpoint features

Advanced Debugging

Getting started with command-line debugging

Using LLDB outside Xcode

Working with crash reports

Summary

Chapter 17: Testing Code with Instruments

Beginning with Instruments

Understanding Instruments

Exploring the Instruments UI

Getting Started with Instruments

Creating a leaky test app

Profiling an app with Instruments

Modifying instrument settings

Comparing different runs

Saving and loading runs

Working with Instruments and Templates

Understanding templates

Creating a template

Creating custom instruments

Working with UIAutomation

Setting up objects to support automation

Creating a test script

Getting started with Automation scripting

Summary

Chapter 18: Working with Testing and Integration

Introducing Unit Testing

Understanding Unit Testing in Xcode

Creating a Simple Unit Test Project

Creating a new project that supports unit testing

Creating a class for testing

Creating a test case

Running tests

Using Bots for Testing and Integration

Getting started with OS X Server

Setting up OS X Server

Creating bots

Monitoring bots

Summary

Appendix A: Using Developer Tools

Finding the Developer Tools

Understanding the Tools

Introducing the Printer Simulator

Working with the Accessibility Inspector

Comparing and Merging Files

Summary

Appendix B: Getting Started with Optional Tools and Utilities

Finding the Tools and Utilities

Understanding Different Versions

Understanding the Tools

Exploring the command-line tools

Getting started with the graphics tools

Introducing the audio tools

Investigating the hardware tools

Exploring the Auxiliary Tools

Summary

Appendix C: Working with Other Editors

Editing plists

Understanding plists

Getting started with plist editing

Editing info.plist project settings in Xcode

Creating new plists

Using the general Xcode plist editor

Editing Core Data Files

Understanding Core Data

Introducing the data model editor

Creating a simple data model

Summary

Guide

Table of Contents

Begin Reading

Pages

1

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

221

222

223

224

225

226

227

228

229

230

231

232

233

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

Introduction

This book is about developing iOS and Mac projects using the Xcode 5 development toolchain. You’ll find this book useful if you have experience with Cocoa or Cocoa Touch and have used Xcode 4 in the past or if you have worked with other development environments and are curious about how to work productively with Xcode 5.

This isn’t a book about languages or frameworks, and the only loose prerequisite is some basic experience with a C-family language. You’ll get the most from it if you download and install Xcode 5 for yourself, work through the examples, and experiment with it as you read.

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!