iOS (iPhone, iPad) Tutorial
iOS is a mobile operating system developed and distributed by Apple Inc. It was originally released in 2007 for the iPhone, iPod Touch, and Apple TV. iOS is derived from OS X, with which it shares the Darwin foundation. iOS is Apple's mobile version of the OS X operating system used in Apple computers.
Audience
This tutorial has been designed for software programmers with a need to understand the iPhone and iPad application development on iOS using Objective C programming.
Prerequisites
Before proceeding with this tutorial, you should have a basic understanding of Computer Programming terminologies. A basic understanding of any of the programming languages, especially Objective C programming language, will help you learn the concepts of iOS programming faster.
General Overview
iOS, which was previously called iPhone OS, is a mobile operating system developed by Apple Inc. Its first release was in 2007, which included iPhone and iPod Touch. iPad (1st Generation) was released in April 2010 and iPad Mini was released in November 2012.
The iOS devices get evolved quite frequently and from experience, we find that at least one version of iPhone and iPad is launched every year. Now, we have iphone5 launched which has its predecessors starting from iPhone, iPhone 3gs, iPhone 4, iPhone 4s. Similarly, iPad has evolved from iPad (1stGeneration) to iPad (4th Generation) and an additional iPad Mini version.
The iOS SDK has evolved from 1.0 to 6.0. iOS 6.0, the latest SDK is the only officially supported version in Xcode 4.5 and higher. We have a rich Apple documentation and we can find which methods and libraries can be used based on our deployment target. In the current version of Xcode, we’ll be able to choose between deployment targets of iOS 4.3, 5.0 and 6.0.
The power of iOS can be felt with some of the following features provided as a part of the device.
- Maps
- Siri
- Facebook and Twitter
- Multi-Touch
- Accelerometer
- GPS
- High end processor
- Camera
- Safari
- Powerful APIs
- Game center
- In-App Purchase
- Reminders
- Wide Range of gestures
The number of users using iPhone/iPad has increased a great deal. This creates the opportunity for developers to make money by creating applications for iPhone and iPad the Apple's App Store.
For some one new to iOS, Apple has designed an application store where the user can buy apps developed for their iOS devices. A developer can create both free and paid apps to App Store. To develop applications and distribute to the store, the developer will require to register with iOS developer program which costs $99 a year and a Mac with Mountain Lion or higher for its development with latest Xcode.
Registering as an Apple Developer
An Apple ID is most necessary if you are having any Apple device and being a developer, you definitely need it. It's free and hence, no issues in having one. The benefits of having an Apple account are as follows −
- Access to development tools.
- Worldwide Developers Conference (WWDC) videos.
- Can join iOS developer program teams when invited.
To register an Apple account, follow the steps given below −
Step 1 − Click the link https://developer.apple.com/programs/register/and select "Create Apple ID"
Interface and Implementation
In Objective C, the file where the declaration of class is done is called the interface file and the file where the class is defined is called the implementation file.
A simple interface file MyClass.h would look like the following −
@interface MyClass:NSObject { // class variable declared here } // class properties declared here // class methods and instance methods declared here @end
The implementation file MyClass.m would be as follows −
@implementation MyClass // class methods defined here @end
Object Creation
Object creation is done as follows −
MyClass *objectName = [[MyClass alloc]init] ;
Methods
Method is declared in Objective C as follows −
-(returnType)methodName:(typeName) variable1 :(typeName)variable2;
An example is shown below.
-(void)calculateAreaForRectangleWithLength:(CGfloat)length andBreadth:(CGfloat)breadth;
You might be wondering what the andBreadth string is for; actually it’s an optional string, which helps us read and understand the method easily, especially at the time of calling. To call this method in the same class, we use the following statement −
[self calculateAreaForRectangleWithLength:30 andBreadth:20];
As said above, the use of andBreadth helps us understand that breadth is 20. Self is used to specify that it's a class method.
Class Methods
Class methods can be accessed directly without creating objects for the class. They don't have any variables and objects associated with it. An example is shown below.
+(void)simpleClassMethod;
It can be accessed by using the class name (let's assume the class name as MyClass) as follows −
[MyClass simpleClassMethod];
Instance Methods
Instance methods can be accessed only after creating an object for the class. Memory is allocated to the instance variables. An example instance method is shown below.
-(void)simpleInstanceMethod;
It can be accessed after creating an object for the class as follows −
MyClass *objectName = [[MyClass alloc]init] ; [objectName simpleInstanceMethod];
Important Data Types in Objective C
| Sr.No. | Data Type |
|---|---|
| 1 |
NSString
It is used for representing a string.
|
| 2 |
CGfloat
It is used for representing a floating point value (normal float is also allowed but it's better to use CGfloat).
|
| 3 |
NSInteger
It is used for representing integer.
|
| 4 |
BOOL
It is used for representing Boolean (YES or NO are BOOL types allowed).
|
Printing Logs
NSLog - used for printing a statement. It will be printed in the device logs and debug console in release and debug modes respectively. For example,
NSlog(@"");
Control Structures
Most of the control structures are same as in C and C++, except for a few additions like for in statement.
Properties
For an external class to access the class, variable properties are used. For example,
@property(nonatomic , strong) NSString *myString;
Accessing Properties
You can use dot operator to access properties. To access the above property, we will do the following.
self.myString = @"Test";
You can also use the set method as follows −
[self setMyString:@"Test"];
Categories
Categories are used to add methods to the existing classes. By this way, we can add method to classes for which we don't have even implementation files where the actual class is defined. A sample category for our class is as follows −
@interface MyClass(customAdditions) - (void)sampleCategoryMethod; @end @implementation MyClass(categoryAdditions) -(void)sampleCategoryMethod { NSLog(@"Just a test category"); }
Arrays
NSMutableArray and NSArray are the array classes used in objective C. As the name suggests, the former is mutable and the latter is immutable. An example is shown below.
NSMutableArray *aMutableArray = [[NSMutableArray alloc]init]; [anArray addObject:@"firstobject"]; NSArray *aImmutableArray = [[NSArray alloc] initWithObjects:@"firstObject",nil];
Dictionary
NSMutableDictionary and NSDictionary are the dictionary classes used in objective C. As the name suggests, the former is mutable and the latter is immutable. An example is shown below.
NSMutableDictionary *aMutableDictionary = [[NSMutableArray alloc]init]; [aMutableDictionary setObject:@"firstobject" forKey:@"aKey"]; NSDictionary*aImmutableDictionary= [[NSDictionary alloc]initWithObjects:[NSArray arrayWithObjects: @"firstObject",nil] forKeys:[ NSArray arrayWithObjects:@"aKey"]];

Comments
Post a Comment