Click here to load reader

Design Then Code - Building IOS Apps From Scratch

  • View
    215

  • Download
    0

Embed Size (px)

DESCRIPTION

iOS App Development Starter Guide

Text of Design Then Code - Building IOS Apps From Scratch

  • Building iOS Apps From Scratchby Mike Rundle

    Before taking a crack at any Design Then Code project tutorials you'll need someknowledge of Xcode, ObjectiveC, Cocoa and UIKit. My goal is for this guide to help bridgethe gap between having no knowledge of iOS development and having enough to starttackling more interesting projects.

    Apple provides a number of tools to enable developers to build Mac and iOS apps. Todownload them, head to the Mac App Store and search for "Xcode". This $4.99 downloadwill give you access to Xcode (the IDE that Mac/iPhone developers use), Interface Builder,the Cocoa Frameworks, tools for testing your apps, and a lot more. To register as anofficial iOS developer and publish apps to the App Store (not to mention testing yourapps on a real device!) it costs $99 per year.

    Here's a quick overview of the tools Apple provides.

    XcodeXcode is an IDE (Integrated Development Environment) used by Mac and iOS developers tobuild applications. It's not just a code editor: it has a variety of additional goodies baked inlike great autocomplete support, static code analysis (it finds bugs in your code before youcompile, including memory leaks) and a variety of debugging and performance tools. You

    could use TextMate or BBEdit and then use commandline tools to do the final compilation, but mostdevelopers choose to do it all within Xcode. I useXcode for all app development.

    Interface BuilderInterface Builder is an application that lets you buildyour interfaces visually. Builtin objects like buttons,tab bars, sliders and labels can easily be draggedonto your app's interface and then configured bytweaking the palettes and panels. You can also use Interface Builder to connect targets andactions (what happens when an interface element is acted on by a user, and what objecthandles the action) as well as manipulate controllers and object bindings.

    In my particular development workflow, I prefer not to use Interface Builder, mostly

    Tools

  • because I work on custom interface components and those still take a lot of code to getexactly right. I recommend that new Mac and iPhone developers get acquainted withInterface Builder, but at the same time still learn the UIKit code that it is generating foryou. I've seen many developers start using Interface Builder and never leave it, so theynever actually learn how to code interfaces from scratch. All Design Then Code tutorialsforego Interface Builder and explain how to write all UI code by hand.

    FrameworksAnd the most important piece of the puzzle: frameworks. Without frameworks and APIsdevelopers wouldn't easily be able to create applications that run on Mac OS X or iOS.Apple provides dozens of frameworks that enable developers to do things like create userinterfaces, write networking code, encrypt important information, draw graphics to thescreen, play audio and video, save data and passwords, take pictures, display webpagesand much more.

    The frameworks that Apple provides let you start off with a rich set of commands andtools upon which to build your applications. Without the various frameworks that Appleprovides, every developer would be reinventing the wheel over and over again. There arelots of goodies given to you in the various Cocoa frameworks so, many times, anincredibly complex thing can be accomplished in only a few lines of code. An example ofthis is automatically fetching a file on the Internet by its URL, parsing this file, thenstuffing it into a data structure that you can manipulate instantly is just a oneliner!.

    These are just the main tools that Apple provides but there are many, many more toexplore and use. Go pick up Xcode from the Mac App Store and start poking around withinyour new /Developer directory.

    Are you totally new to computer programming? Have never even written any JavaScript?This tutorial might be tough to swallow. Take a look at the following list of terms andexamples:

    Variable var x = 15;

    Function var name = John.getName();

    Loop for (var x = 0; x < 10; x++)

    Conditional if (x == 20)

    Array var things = array("dog", "cat");

    If any of these are confusing to you, I'd suggest heading to the JavaScript tutorial at

    Introduction To Programming?

  • w3schools.com and working through the first few sets of chapters to get a quick feel forgeneral computer programming constructs. If you're coming from a language that doesnot look like C (Lisp, Ruby, etc.) or if you have no C programming knowledge (what's aheader file? what's a struct?), I'd recommend taking a quick read of Scott Stevenson'sexcellent C language tutorial and then coming back after your brain is thoroughly soakedwith knowledge.

    Now let's talk about ObjectiveC.

    ObjectiveC is the language that most Mac and iOS developers use to write nativeapplications. There are alternative ways to write Mac and iOS apps (pure C or C++,MacRuby, PyObjC, MonoTouch, etc.) but we won't be exploring those avenues here. Apartfrom these programming languages, it's also possible to build "apps" that aren't nativelycompiled for the platform but are instead websites made to look like a native app butloaded in a web browser. Those apps can be written using a variety of frameworks butthey're primarily built using web technologies including JavaScript, HTML and CSS.

    If you're looking to build mobile websites or web apps then you don't need to learnObjectiveC or Cocoa and probably don't need to go any farther in this tutorial. If you'relooking to learn a new programming language and build native apps for the Mac and iOS,then this is the place for you!

    ObjectiveC is an objectoriented programming language that is essentially a thin layer ontop of C. It adds Smalltalkstyle messaging, runtime reflection, inheritance and many otherthings to the C programming language. Its syntax is very unique and may be confusing atfirst to seasoned developers used to languages like Ruby or Java. Because ObjectiveC is asuperset of C, developers can "drop down into" C at any point in their code if they wish.Many parts of a Mac or iOS application will utilize native C function calls and primitive Cdata types right next ObjectiveC methods and data types.

    Classes, Objects & MethodsIn case your objectoriented programming fu is rusty (or totally nonexistent) we shouldfirst define what classes, objects and methods are and how they relate to each other inObjectiveC.

    A class is a blueprint that describes the state and behavior of a particular set of objects. Ittypically models a concept or a thing from the real world, for example, an Animal. AnAnimal class would probably define some variables like the number of legs it has, itsheight and weight, and also behaviors, like running, eating and sleeping. An object of agiven class is called an instance of the class. Each new instance you create can have itsown values for the data. You could create 100 instances of the Animal class and set eachone's variables to something different. Methods are behaviors a class possesses, and in

    Introduction To Objective-C

  • When working with variables that have anexplicit, set size at compile time, they're storedon the stack. The stack stores local informationand you can access its data without pointers.Primitive data types (int, char, float, etc.)have a defined, maximum size so they're storedon the stack.

    In ObjC, objects don't have a maximum size(they can grow as much as you need them to)so their memory must be dynamically allocatedon the heap and referenced with pointers. Apointer is an address in memory, and to accesswhat's actually stored in that memory position,you dereference the pointer to get at itscontents.

    Starting in Xcode 4.2, there's a new feature thatCocoa developers can use called AutomaticReference Counting (ARC) which takes all theeffort out of manual memory management. It'sa compiler feature which will automatically addin calls to -release for you so you don't ever

    ObjectiveC, you can call a method directly on the class itself or on an instance of it. Thesetwo types of methods are called class methods and instance methods.

    When you want to create a new instance of a class, you have to first allocate and initializea block of memory for it on the heap. The heap is memory set aside for dynamic allocationand in ObjectiveC all objects live on the heap. All ObjC objects are pointers to this blockof memory and an asterisk (*) denotes that a variable is a pointer type. Here's an exampleof creating an instance of our Animal class.

    Animal *myAnimal = [[Animal alloc] init];

    Let's break this down piece by piece. The firstthing you'll probably notice are the squarebrackets. They're everywhere in ObjectiveC andthey encapsulate method calls. On the left side ofthe line we're creating a new Animal* variablenamed myAnimal, then, on the right side, we'redoing 2 different method calls (one nested withinthe other) and assigning what they return tomyAnimal.

    To understand what's going on we need to look atthe method calls from the insideout. First, we'recalling [Animal alloc] which means we'recalling the class method +alloc directly on theAnimal class. Class methods are preceded with aplus sign. This method returns a generic object type of id. Then, we call the instancemethod -init which initializes the memory, allowing us to actually use this new object.Instance methods start with a minus sign. Objects in ObjectiveC aren't fully ready to beused unless these two steps are taken.

    An important concept to understand when working with objects is that if you allocatedmemory for it, you're also responsible for letting the runtime know when you're doneusing the object so that its memory can be released. An object can be in use in differentparts of an app at once, so ObjectiveC uses a reference counting system to keep t