iOS Programming Tutorial: An Introduction to Xcode

iOSHave you ever had a great idea for an iOS app but didn’t know how to start? Turning that great idea into an actual app requires lots of planning and training and we have a solution for you! You need an iOS programming tutorial that will teach you how to decipher and use design patterns, code writing, and other technologies required to turn your idea into reality. Starting with a rudimentary knowledge of programming, preferably a C-like language such as Objective C or even C itself, this iOS programming tutorial will guide you from the characteristics of the process to display constraints to the heart of Xcode functions.

Taught by Vishal Lamba, owner of EDUmobile, his complete lecture will go through the entire iPhone Apps Development Cycle from start to finish without the pain and frustration most students go through on their own. The lecture transcript below will walk you through a brief introduction on what you need to understand primarily to program in Xcode. You will use a simple template to create, adjust, and customize a simple application. By the end of this lecture, you should be able to build and manipulate a simple application and then move on to more complex concepts such as controls and multimedia features in the later sessions. Then you will be on your way to mastering iPhone programming in no time!

Lecture Transcript:

Hello and welcome. Today, we’re going to have a brief introduction to programming in Xcode using iOS 5, or the iPhone. We’re going to talk about what you will need to know, what you will need to have, some programming characteristics of the iPhone device and programming for it, the Xcode interface itself, how it looks and how we achieve programming in that interface, and finally, we’re going to build a basic application. So, let’s get started.

First of all, you’re going to need to have an Intel-based Mac computer, running either OS X 10.6.7 – Snow Leopard – or OS X 10.7 – Lion. Lion was released just this summer while Snow Leopard has been out for awhile. But you will need at least a base version of 10.6.7 of Snow Leopard to program. You’ll need the iOS SDK of version five, which is downloadable for free at this site: developer.apple.com/iphone. And you want the SDK download that contains Xcode 4.2 at this writing, or the latest version of Xcode. That is our development environment – our IDE – and it contains simulators for both iPhone and iPad, so you can test your development in a safe environment before porting it to an actual device.

You’ll also need to have some knowledge of programming, specifically some knowledge of programming a C-like language, or a language with a C-like syntax, preferably C itself. If you have programmed in an object oriented programming language, that’s a definite plus to you, but at least you should have some awareness of what such concepts as Encapsulation and Inheritance, and Polymorphism of R. And we’ll be making extensive use of those concepts in this course. You also need some knowledge of variables, looping structures in that C-like language syntax, as I already mentioned. And of course, some familiarity with the iPhone device itself. If you own one, you’re ahead of the game; you already know what the touch interface feels like, works like, and you have a grasp of the idiom of using these devices. As you program, you need to keep the user in mind, of course. It’s the number one rule in programming. Having knowledge of how the device works allows you to write programs that are more typical for the users of that device.

As we write programs for the iPhone, what are some of the characteristics of that programming process? Well, we’re using a framework called Cocoa, which is Apple’s framework for development, not only on mobile devices, but also for desktops on the Mac computer. It is very different from .net or Java programming that you might be used to. If you have programmed for a Microsoft phone using .net or Silverlight, or in Java for the Android, then there will be some differences that you will notice immediately. First of all, we’re developing in a language called Objective-C. Objective-C is a strict object oriented super set of the C language. Anything and everything you can say in C, you can say in Objective-C. The Objective-C language just simply adds object oriented concepts and constructs to the C language, and it does it in a very streamlined way. I think once you get used to some of the syntactic elements that you’ll be very happy with using the language.

Only one application at any given time is running on an iPhone or an iPad. All other applications go into a suspended state. The application will pick up right where it left off when you touch the icon for the application again, unless of course the application has crashed or iOS has marked it for memory capture. That’s rare, but it does happen. If it does happen, we have methods that will allow us to save state and restore that state when the application comes back up.

Every application, of course, runs in its own sandbox. We can’t go outside of our sandbox. By that I mean that one application running cannot talk to another application running. Remember, only one application is running at a given time. Aside from that concern, applications can’t send or receive information from other applications at a user-run level. An application does have access to what are called devices on the iPhone. Those devices are things like the Accelerometer, the camera, the photo book, the address book, things like that, and we do have access through APIs to those, but we can’t talk back and forth between two applications at that level.

Of course, there are display constraints. If you’re used to programming for desktop computers, you don’t have the display constraints, or at least they’re not this small. Points are bigger than pixels in a retina display. We think in points, we program in points. We are looking at 320 x 480 points on the iPhone and 1024 x 760 points on an iPad. The touch interface, of course, for user interaction, again, you must keep this in mind and program toward having a touch interface, rather than a keyboard and mouse interface. And that becomes very obvious, and it actually is very simple to do. At least to get basic interaction going, it’s extremely simple to do. In fact, Xcode will handle that for you.

The Xcode interface itself, we’re going to program a Hello, World application. The reason we’re doing this is to show you the process of working in Xcode, and we’re going to get right to that here in a moment. But first of all, I will list some things for you to look for as we run through this exercise. We want to look at how we start a new project, and we’re going to look at the parts of the Xcode interface, the main project window, and the editors we use. We use more or less mostly two editors, a code editor, and what’s called an interface builder. And then there’s some folders that are very important; the main program folder, the supporting files folder, and the frameworks and products folders that we’ll look at in a little bit.

As mentioned, we’re going to build a Hello, World application. Hello, World is typically the first application you build when you’re starting to look at a new programming language, and you do it for a couple reasons. First of all, you want to make sure that everything’s running and that everything compiles correctly before you proceed to more difficult or more fully fledged applications. So what we’re going to have is a single view. A view is a visible container for other views. So, we’re going to have a single top level view. It will contain a UI label component. A UI label is a view, and it has a property called text. So, we’re going to take the color of the view and change it. We’re going to add a label component to the view, and we’re going to set that label’s text property directly in Interface Builder. And then we’re going to build and run the project.

So, let’s go ahead and do that. I’m going to run Xcode now, and we’re going to create a new project. It will ask us what kind of project we want to create. For this example, I’m going to create a single view application and click next. I’m going to call it, Hello, World, and click next. Notice in that last window the device family is iPhone. If this box, Include Unit Tests, is checked, please un-check it for this example. And then, we’re going to use Story Board for this, and we can see that Story Board is a new concept in iOS 5. Previously, we were using Views, and we’re still using Views, but the concept of Story Board is that we hold, or encapsulate the window and all of its views into a single Story Board file. It streamlines development to some extent. If you have been working iOS 4 – some version of that, or before – you’ll see some differences with this. Once you get used to it, I think you’ll really appreciate the differences that they’ve made, or the changes that they’ve made.

Now, we’re going to save our application and Xcode is going to use templates to create a very simple application. It does a lot of work behind the scenes, and we’ll look into that as we discuss Objective-C in depth in the next few lessons. But the things to look at: we’ve got some project level settings in this window, and across the top we can get to informational settings and build settings and phases and rules. Over in this side, in this navigator window, we have a list of folders. We’ll see a folder named after the project, and that folder is going to contain two things. It’s going to contain our Story Board file, it’s going to contain the classes for our application, and it’s also going to contain a sub-folder called Supporting Files. Supporting Files is where you would put resources for your application that are outside of the application build or built with the application, but outside of the scope of building in Xcode, such as images or music resources or things like that.

It contains some informational files, this helloworldinfo.plist and info plist strings and a PCH file. The most important file in here right now for our concern is Main. And Main, it contains the C language main function that will start off the entire running process. We’ll be looking at that in our next session.

We also have a Frameworks folder. Frameworks – if we expand that – contains the frameworks required to run and display the application, and this is Cocoa. There is a framework called UI Kit, a framework called Foundation, and a framework called Core Graphics, and we’ll talk about each one of those and what they do and what pieces of Cocoa are present in each.

We have a Products file, and the Products file contains the build target. If we were building for multiple targets, there would be multiple products here, but for now we have just one since we’re only building for the iPhone.

So, let’s take a look at this mainstoryboard.storyboard file. And we see a graphic user interface that displays that for us. We’re going to open this third button in the view cluster of buttons here. This third button opens up, depending on the editor that you’re in, and it will open up properties for that object. Right now, we’re looking at the properties of the Hello, World view controller object, which is the object that contains the view. Now, the view, if we select that, we see that we can edit the views properties. This Hello, World view controller however, is actually these two files. The header file declares and the IM file – or the implementation file – defines that controller object.

So, we’ll go down to the view, and over here we can see that we can change the background color. And we’ll change that to something cheerful. And then we’ll drag a label object to our view. We’ll change its size so that it is all the way out to the user interface guidelines on both sides, and we’ll make it a little bit bigger vertically. And we’ll change the minimum font size to 30. Notice that also changes the system font size to 30. Then we’ll center the text inside of the label. There’s much more detail that we can go into concerning these properties, their metrics, how they’re connected to one another, and we’ll definitely do that soon. But for now, just to illustrate, we’re just doing a very simple Hello, World program.

When we run this program, we don’t want it to say Label; we would like for it to say Hello, World. So, in the text property of our label, we’ll select that text and we’ll change it to Hello, World. Hit enter, and that’s reflected in the text of our label. If we go to the product menu, you can see that there’s a build selection – command B – and we can build it there, or use the keystroke combination. We get a progress indicator that shows us the build progress, and very shortly that will complete. We get a small box down here that says, Build Successful, or we get some error messages and they will pop up in this fourth area right here if our build was not successful.

The next thing we want to do is run our application. Again, in the product menu, we can see the run selection, or we can just click this button, which will actually do a build and then a run, and sometimes we do that. It is normally better to save if you’re going to do a build and run, but by default, saving happens automatically.

Now, the iOS Simulator, the iPhone simulator will run first, and then it’s going to attach to this Hello, World program. The first time this runs, especially if the simulator isn’t up already, it takes a few moments to start. But we see this message attaching to Hello, World, running Hello, World on iPhone simulator. And there we go.

So, what do we have now? We have changed the color of the view, we placed a label on the view, we’ve altered its text, and then this application is running. Again, this is a very simple application, and it doesn’t do much, if anything at all, except display some text. If we click the Home button, the application will fade into the background, and its icon is here. Now, that application is not running; it’s in a suspended state. If we click the icon again, then we get the application, which comes back right where it started. Now, you can’t see any difference. There is nothing happening, but if there were activity in the application, you would see that it would come back right where it started.

That’s a very simple introduction to Xcode, and the very simplest of applications. I hope you enjoyed it, and we’ll be talking soon. Thank you.

Learn iOS programming with this “concept and problem mechanism” course model today!