Your SlideShare is downloading. ×
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Free intro to iphone Programming eBook
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Free intro to iphone Programming eBook

2,498

Published on

Many experts agree that EDUmobile's complete iPhone Programming course may be the best course online. Already, over 350 students have enrolled and are under training from over 50 countries …

Many experts agree that EDUmobile's complete iPhone Programming course may be the best course online. Already, over 350 students have enrolled and are under training from over 50 countries worldwide!

With this beginner-friendly ebook, you'll gain a basic understanding of iPhone Programming and see a walkthrough of a simple Hello World App.

This book has 34 pages of fantastic content that will walk you through some of the basics of iPhone Development and introduce you to the path you will have to take if you want to successfully start developing iPhone Apps that you can convert into cash!

Sign up at the link below to get your free copy!

http://www.edumobile.org/iphone-programming-ebook-free.php

This is an Introductory Chapter from the highly popular EDUmobile iPhone Development Training Program.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,498
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
111
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Beginning Your iPhone Programming Journey An Introductory Chapter from EDUmobile.ORG iPhone Development Training Program Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 2. NOTICE: You Do NOT Have the Right to Reprint or Resell This eBook! You Also MAY NOT Give Away, Sell or Share the Content Herein Copyright © 2009 – 2010 EDUmobile.ORG ALL RIGHTS RESERVED. No part of this ebook may be reproduced or transmitted in any form whatsoever, electronic, or mechanical, including photocopying, recording, or by any informational storage or retrieval system without the expressed written, dated and signed permission from the author. LIMITS OF LIABILITY / DISCLAIMER OF WARRANTY The authors and publisher of this book have used their best efforts in preparing this material. The authors and publisher make no representation or warranties with respect to the accuracy, applicability, fitness, or completeness of the contents of this program. They disclaim any warranties (expressed or implied), merchantability, or fitness for any particular purpose. The authors and publisher shall in no event be held liable for any loss or other damages, including but not limited to special, incidental, consequential, or other damages. This manual contains material protected under International and Federal Copyright laws and Treaties. Any unauthorized reprint or use of this material in prohibited. Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 3. Beginning Your iPhone Programming Journey Every once in a while a new opportunity arises due to a revolutionary technology and business model which is years ahead of the current scenario. Apple has done exactly that with its iPhone and iPod products, content delivery mechanism and business model that has leveled the playing field between big, small and individual businesses. Those of us who move quickly to learn the technology are the ones who will benefit the most from it, before ideas are taken by others and before the market gets too saturated, making it harder to succeed. You may have a very exciting idea and a passion to learn the tricks of coding like the gurus, but the due to limited iPhone development tutorials online and lack of well structured courses, your journey may probably not take off at all. As a result, your idea that could become the next blockbuster application on the App Store – will never happen and remain just a dream. And, its not really your fault… as there are currently no affordable online iPhone development courses that teach you through real tutors who guide you through step by step. This is where the iPhone Course offered at EDUmobile.ORG comes in… Over a period of 12 weeks you will learn the topics step by step and start coding like the iPhone gurus. We will teach you through the following mediums - Weekly Online Videos that you can download and view at your convenience Access to Real Tutors to help you solve coding or conceptual issues, on a one-on-one basis Weekly Worksheets and Exercise for you to do with challenging examples that are set up with just the right learning curve Access to an Online Form for discussing issues and meeting other students. One or Two Live industrial projects at the end of the course Plus you get a full 30 days no questions asked full money back guarantee. All this said, now lets get started … Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 4. First things first… is the iPhone Programming Course right for me? Before starting your journey, you must understand what is needed and expected of you to finish the course successfully. If you have absolutely no knowledge of programming, it will be very hard – although not impossible. Since, this is a programming course, you will be better prepared if you have programmed before – in any language. It does not matter if you programmed in Java, C or Scripted websites. As long as you know the basics you are good to go. However if you are not a programmer, but are up to the challenge, then you must ask yourself if you will be programming yourself - or will you go through this course to eventually become a producer for an iPhone product. All said - a very basic knowledge of programming is a must. I suggest that you try to learn just basic C or python to quickly learn the programming nuances and concepts. If you are from a completely non-programming background, you may need a week to 1 month to catch up with the basics depending on the time you spend on learning. So, in order to get started comfortably with this course, you should be able to understand the following concepts before you go forward - What are data types? What are operators? What are conditionals? What are loops? What are arrays and strings? What are objects? What are methods? Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 5. I’ll stop at that, for now. This should be more than enough. I suggest you know at least a few of the above, if not all, and they should not sound completely alien to you! Of course, if you need to review some of these concepts, you can continue brushing up your programming fundamentals in parallel to the iPhone course. Either way … you need to do some homework before you start the iPhone Programming course! Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 6. How is the Course Structured? The iPhone Course is structured to teach you and focus on the following - Objective C Cocoa Touch Framework iPhone SDK iPhone APIs Objective C Objective C is a programming language which will be used to program on the iPhone. If you have programmed for Mac before you already know Objective C and for those who are new Objective C you can think Objective C to be child of C programming language and a distant cousin of C++. It was developed by Brand Cox and Tom Love for their company StepStone. According to Wikipedia Objective-C is a reflective, object-oriented programming language, which adds Smalltalk-style messaging to the C programming language. Now if you don’t understand what is reflective, object oriented and messaging there is no need to panic. It will be discussed soon and you will cross these mole hills before you actually climb the mountain. Cocoa Touch Framework Cocoa touch is Apple's name for the collection of frameworks, APIs, and accompanying runtimes that make up the development layer of iPhone OS. By developing with the Cocoa touch frameworks you will be writing applications the same way that iPhone OS itself is written, with controlled access to the the operating system. You need to learn various aspects of the touch framework to master the iPhone programming. Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 7. iPhone SDK The iPhone SDK consist primarily of Xcode and Interface Builder. The Xcode is all season complete SDK for building MAC software. It has been extended to support the iPhone and iPod programming. The other tool that will be used very often is called the interface builder. It helps creating faster interface for your application with minimal coding thus reducing the development time considerably. iPhone APIs Additionally to Cocoa there are other libraries such as OpenGL es which you need to learn to give your application that extra punch which can knock down the competition. The other iPhone specific components that you need to learn is SQLite , Core animation, Accelerometer, phone and media playing APIs. Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 8. Understanding The iPhone SDK The first thing we need to do to get started is to setup our coding environment on our computer. For this, we need to download and install the iPhone SDK on our Mac or the Virtual OS on our Windows PC. Installing the SDK Download the SDK from http://developer.apple.com/iphone after registering at the portal. The registration is free. If you want, you can optionally join the developer program for $99 only, but we suggest you do that later. Once the Xcode is installed, it will appear in the dock as shown below. You need to click and start the Xcode to create your first application. Alternatively, you can search for Xcode in Finder and can start things from there. Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 9. Once Xcode is started, the following screen will be visible on your desktop… Now, close the smaller window, and you are ready to start programming for iPhone! Next, do the following…. Access – File >> New Project … and a new project template window will be displayed as shown on the next page, below - Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 10. Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 11. You will see that there are ready made templates for creating applications. These templates are handy to use and more often than not, we will use standard templates to create our applications. However, if you do not need any hand holding and you want to create an application from scratch, you may use the windows based application. For our example today we will select … “View Based Application”. ... and give a name to the Application as “Hello World” . The following screens will appear… Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 12. However, before we start any work on our application we need to familiarize ourselves with the Xcode IDE. So what exactly is Xcode IDE ? Xcode IDE primarily can be narrowed down to three major parts in addition to the Menu. They are … The Top Bar The Side bar The code editor Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 13. 1. The Top Bar This customizable bar provides shortcuts to menu elements, and looks like this… 2. The Side Bar This bar displays the directory structure of the current project. We will use it more in a later part of our course. It looks like this … Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 14. 3. The Code Editor The other major part is the code editor. The content area is where you write your code. You can access the files from the side bar by clicking them and it will show up in the content window. Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 15. Application Architecture - A Sample Application When we created the views based project, Xcode create lots of code and component for us. Let us have a detailed look into this code. If we click on the Classes folder on the left panel we will see the classes that have been created by the Xcode. These classes are… Hello_WorldAppDelegate.h Hello_WorldAppDelegate.m Hello_WorldViewController.h Hello_WorldViewController.m These are all Objective C files, each playing a designated role in the project. Through in this article we will not get into the details of these files but, for clarity of purpose, we will define the work assigned to these classes. We know that “.h” files in objective C are for deceleration and “.m” files have the implementation details. So the two classes Delegate and View Controller are also divided into respective “.h” and “.m” files. The Delegate class is the class which listens to the Applications running. This class actually starts the application and exists all through the life cycle of the application. In short you can consider the Delegate as the Shell which enables the application. Application passes messages on to the delegate through its life cycle which is handled and acted upon by it. The View Controller is very specific to view based application that we have selected. Before we discuss the View Controller we must tell you that View is necessary to draw content on the iPhone. In fact each iPhone Application has one window and can have multiple views. A view is drawn over a window and in most of our practical applications we will be dealing with views only. Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 16. A view is something that is visible to user and the controller decides what should be shown on the view. This architecture is similar to the MVC architecture where we use model view and controller components to create our application. Auto Generated Code Example Let us now have a detail look into the code generated by the Xcode. In order for it to make any sense you must understand ObjectiveC. However, even if you are not familiar with this, as of now, you can read through it. We have briefly clarified most of the major concepts. Before we look into the code let us just do a bit of Objective C. Objective C is Objects added to C. It is quite different from C++ in terms of its syntax and is rather similar to Smalltalk. However, it contains most of the object oriented features like Data Encapsulation, Polymorphism and Inheritance. Now, let us talk about some important concepts which are there is the code below @interface - The interface in objective C is use to declare the methods and data elements of the class. @implementation - The implementation defines the methods that were declared earlier using the interface. @class - Declares the methods defined elsewhere. It is less memory intensive than an #import statement and in a compact device like iPhone it will used more often than mac applications. @property and @synthesize - The property and synthesize directive add getter and setter function to the class. A getter and setter function is use to assign and obtain values to an object. When we use @synthesize we do not have to explicitly define a get and set function for the object properties it just get internally defined by the system. Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 17. IBOutlet - It is a special instance variable that references another object. It lets the Interface builder know of the object. We will discuss it in detail in later weeks. IBAction - It is the trigger which is called from an Interface builder UI object. Now that you are familiar with the above concepts, here is the code snippet… AppDelegate.h @class Hello_WorldViewController; @interface Hello_WorldAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; Hello_WorldViewController *viewController; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet Hello_WorldViewController *viewController; @end Explanantion of the code above… The Above delegate class starts with defining a class directive for view controller. As discussed above it does that to import the definitions from view Controller class. The Next line uses a concept called as Protocol - @interface Hello_WorldAppDelegate : NSObject <UIApplicationDelegate> A protocol is list of methods shared between classes. UIApplicationDelegate is the protocol and the NSObject is the parent class of Hello_WorldAppDelegate. Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 18. Next, we create object of UIWindow and View controller. Then, we make these objects properties. The key words “nonatomic” and “retain” decides the way the object is stored and accessed. We will talk about them a bit later. This concludes our explanation of AppDelegate.h code. Now, lets Move on to our Second code example AppDelegate.m #import "Hello_WorldAppDelegate.h" #import "Hello_WorldViewController.h" @implementation Hello_WorldAppDelegate @synthesize window; @synthesize viewController; - (void)applicationDidFinishLaunching:(UIApplication *)application { // Override point for customization after app launch [window addSubview:viewController.view]; [window makeKeyAndVisible]; } - (void)dealloc { [viewController release]; [window release]; [super dealloc]; } @end Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 19. Explanantion of the code above We know that .m file is the implementation file. We start by importing the “.h” files. This is required for the compiler to relate the objects to the corresponding class. We then synthesize the two properties we created earlier. Please note we will have to synthesize any properties before using them. We then find a method which is first called as soon as the application starts. The method “ applicationDidFinishLaunching” is created automatically the SDK. This starts the application with the root window. It then adds the view on top of the window with the key word “addSubview”. As our view is being handled by the object viewController the system added addSubview:viewController.view. The next method dealloc is to free the memory occupied by the objects we created. Third Code Example Snippet ViewController.h #import <UIKit/UIKit.h> @interface Hello_WorldViewController : UIViewController { } @end Explanantion of the code above Currently this class has very few lines and it just declares itself as an interface. Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 20. Fourth Code Example ViewController.m #import "Hello_WorldViewController.h" @implementation Hello_WorldViewController ////Some methods - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)dealloc { [super dealloc]; } @end Explanantion of the code above … Currently the code is not doing much except for being an empty implementation file. The methods that are there are empty and does not serve any purpose as of now. We will now take this program to the next level adding an interface to our application and we will do it by using Interface Builder. Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 21. Though it is possible to add interfaces without using Interface builder by directly using the objective C code, but it greatly simplifies the development process and reduces the time to a greater extent. Also, the aim of this article is to make you familiar with IB - and so we will create our interface with interface builder. The “Interface Builder” This is the interface creating utility that comes with Xcode for developing interfaces. To see it in action, in your side bar click the resources folder and click on the ViewController.xib file. The xib file is an interface file which is called a “nib file”. Once you double click on the nib file it will automatically open the interface builder. You will see the following on your screen… The Interface builder has three main parts, namely - Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 22. 1. Component Window This shows the components of the interface. In our example we have… The File’s Owner, First Responder and View, explained in a nutshell… At this point of time you can consider these element as components by which the interface is created and rendered on a device. The File owner is the master class and the interface we creates belong to the File Owner. The First Responder is responsible of responding to user inputs done at the interface. We will deal with them in detail in our later sections. The View is the actual interface that will be visible to the user. Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 23. 2. Library The library is the collection of interface elements like Label, TextBox and even view controllers. You can create great applications just by using these standard components. Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 24. 3. Inspector The inspector is the other major component of the interface builder. It allows the developer to change various attributes of the interface object. Using the Inspector we can change the following attributes - View Attribute Connection Attribute Size attribute Identity attribute. Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 25. If you want to have a look at these attributes, select the Inspector from the Drop down menu and the window will appear in your screen. Then, select View and the Inspector window will show different attributes for each tab as listed above. We will learn to use this a little later. We will first complete our application which will print “Hello World” on the window. Completing the Application Open the library from the Xcode menu. Go to the element label and drag it to the view window which is currently gray in color. You will see the following ... Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 26. Once your label is present on the view, you can clearly see the word “Label” written on the view. Moving forward, our task is to now convert this label to “Hello World”. And… that is done very easily! Simply double click on label and you can edit it to whatever you want. Alternatively you can select the label and open up the Inspector. In view attribute, that is first tab itself, change the text from label to Hello World. Save the nib file and close the nib file by closing the component window. So, now that we are done, we want to execute the first program we wrote! In your Xcode top bar, click “Build and Run” and if everything is proper, you will see the emulator appear and display the text… “Hello World” on the screen, just as shown in the figure below… Wasn’t that hard, was it ? Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 27. Lets Move on to our Next Exercise As we have now successfully created our first Application we will try to extend this application and will try and add a user event to it. To do this, we will have to augment the code created by Xcode. What we want to do is to insert a button in the interface which can be clicked by the user, and we should be able to change the message, as per the click. We will start by creating an IBOutlet for the label which we want to change and an IBAction to interpret the user action. Make the following changes to Hello_WorldViewController.h #import <UIKit/UIKit.h> @interface Hello_WorldViewController : UIViewController { IBOutlet UILabel *label; } @property (nonatomic, retain) IBOutlet UILabel *label; -(IBAction)buttonPressed; @end The code above creates an Outlet of class UILabel to store the label object which has been defined as *label. We then make this object a property to automatically use getter and setter functions later. We then create a -(IBAction) buttonPressed trigger which will be called when user presses the button element on the screen. We now move on to the Interface builder, to create the necessary interface. Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 28. Double click the view controller nib file to open the interface builder. Open the library and drag a round rectangle button from the library to get the following view… Click on the button and change the name to switch. Now, we will associate this label with our UILabel object. Control drag from the File Owner to the Label which is now written as “Hello World” and from the pop up that appears select “label”. “label” was the object we created in our code. Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 29. Refer to the two images below - Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 30. Once our IBOutlet for label is set we will move to add the IBAction to button. To connect the IBAction we will select the button and open up the Inspector. Go to the connection attributes and drag “Touch up inside” to the File Owner. Select the action “buttonPressed” from the pop up. If the connection is successfully joined your Connection Inspector should look like the following… Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 31. Once this is done save your nib file and move on to the Xcode. Now, open the Hello_WorldViewController.m file and add the following code… #import "Hello_WorldViewController.h" @implementation Hello_WorldViewController @synthesize label; -(IBAction)buttonPressed{ if([label.text isEqualToString:@"World Hello"]) label.text = @"Hello World"; else label.text = @"World Hello"; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)dealloc { [label release]; [super dealloc]; } @end We start by synthesizing the label object. In the IBAction method we apply a simple conditional to check the current string and then change the string based on the current stage. This completes our Application which displays hello world and then changes itself based on the user command. Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 32. The output should look like this … Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 33. This concludes our introductory tutorial for iPhone Programming! We hope you enjoyed it! You may download the source code for the above project from the following link - http://edumobile.org/downloads/Sample.zip So,,, What Next ? This was a small step in our journey towards getting started with iPhone development. If you are interested in learning iPhone Development, we suggest you sign up for our complete 12 Week iPhone Programming Course at the link below. It comes with a full money back guarantee, so there is no risk at all. Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved
  • 34. You will be assigned your own personal one-on-one tutor, whom you may consult any time with questions and problems you face. The course is delivered over a period of 12 weeks via Online Video that you may download and view at your convenience, along with material and Weekly Worksheets. You will also get access to our private forum where you can meet other fellow iPhone developers. Click Here To Learn More About The iPhone Course and To Enroll Affiliates Join Our High Paying Affiliate Program and Make Tons Of Money! Interested in promoting our Courses and making money? Get instant $25 bonus for signing up! Click Here to sign up for our Affiliate Program Copyright © 2009 – 2010 EDUmobile.ORG All Rights Reserved

×