iOS – Camera Management

iOS – Camera Management ”; Previous Next Camera is one of the common features in a mobile device. It is possible for us to take pictures with the camera and use it in our application and it is quite simple too. Camera Management – Steps Involved Step 1 − Create a simple View based application. Step 2 − Add a button in ViewController.xib and create IBAction for the button. Step 3 − Add an image view and create IBOutlet naming it as imageView. Step 4 − Update ViewController.h as follows − #import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIImagePickerControllerDelegate> { UIImagePickerController *imagePicker; IBOutlet UIImageView *imageView; } – (IBAction)showCamera:(id)sender; @end Step 5 − Update ViewController.m as follows − #import “ViewController.h” @interface ViewController () @end @implementation ViewController – (void)viewDidLoad { [super viewDidLoad]; } – (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } – (IBAction)showCamera:(id)sender { imagePicker.allowsEditing = YES; if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) { imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; } else { imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; } [self presentModalViewController:imagePicker animated:YES]; } -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; if (image == nil) { image = [info objectForKey:UIImagePickerControllerOriginalImage]; } imageView.image = image; } -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [self dismissModalViewControllerAnimated:YES]; } @end Output When we run the application and click show camera button, we”ll get the following output − Once we take a picture, we can edit the picture, i.e., move and scale as shown below − Print Page Previous Next Advertisements ”;

iOS – Objective-C Basics

iOS – Objective C ”; Previous Next The language used in iOS development is objective C. It is an object-oriented language and hence, it would be easy for those who have some background in object-oriented programming languages. 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”]]; Print Page Previous Next Advertisements ”;

iOS – Environment Setup

iOS – Environment Setup ”; Previous Next iOS – Xcode Installation Step 1 − Download the latest version of Xcode from https://developer.apple.com/downloads/ Step 2 − Double click the Xcode dmg file. Step 3 − You will find a device mounted and opened. Step 4 − There will be two items in the window that”s displayed namely, Xcode application and the Application folder”s shortcut. Step 5 − Drag the Xcode to application and it will be copied to your applications. Step 6 − Now Xcode will be available as a part of other applications from which you can select and run. You also have another option of downloading Xcode from the Mac App store and then install following the step-by-step procedure given on the screen. Interface Builder Interface builder is the tool that enables easy creation of UI interface. You have a rich set of UI elements that is developed for use. You just have to drag and drop into your UI view. We”ll learn about adding UI elements, creating outlets and actions for the UI elements in the upcoming pages. You have objects library at the right bottom that consists the entire necessary UI element. The user interface is often referred as xibs, which is its file extension. Each of the xibs is linked to a corresponding view controller. iOS Simulator An iOS simulator actually consists of two types of devices, namely iPhone and iPad with their different versions. iPhone versions include iPhone (normal), iPhone Retina, iPhone 5. iPad has iPad and iPad Retina. A screenshot of an iPhone simulator is displayed below. You can simulate location in an iOS simulator for playing around with latitude and longitude effects of the app. You can also simulate memory warning and in-call status in the simulator. You can use the simulator for most purposes, however you cannot test device features like accelerometer. So, you might always need an iOS device to test all the scenarios of an application thoroughly. Print Page Previous Next Advertisements ”;

iOS – Home

iOS (iPhone, iPad) Tutorial PDF Version Quick Guide Resources Job Search Discussion 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. Print Page Previous Next Advertisements ”;

iOS – Sending Email

iOS – Sending Email ”; Previous Next We can send emails using the Email application of iOS device. Steps Involved Step 1 − Create a simple View based application. Step 2 − Select your project file, then select targets and then add MessageUI.framework. Step 3 − Add a button in ViewController.xib and create an action for sending email. Step 4 − Update ViewController.h as follows − #import <UIKit/UIKit.h> #import <MessageUI/MessageUI.h> @interface ViewController : UIViewController<MFMailComposeViewControllerDelegate> { MFMailComposeViewController *mailComposer; } -(IBAction)sendMail:(id)sender; @end Step 5 − Update ViewController.m as follows − #import “ViewController.h” @interface ViewController () @end @implementation ViewController – (void)viewDidLoad { [super viewDidLoad]; } – (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)sendMail:(id)sender { mailComposer = [[MFMailComposeViewController alloc]init]; mailComposer.mailComposeDelegate = self; [mailComposer setSubject:@”Test mail”]; [mailComposer setMessageBody:@”Testing message for the test mail” isHTML:NO]; [self presentModalViewController:mailComposer animated:YES]; } #pragma mark – mail compose delegate -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{ if (result) { NSLog(@”Result : %d”,result); } if (error) { NSLog(@”Error : %@”,error); } [self dismissModalViewControllerAnimated:YES]; } @end Output When we run the application, we”ll get the following output − On clicking Send Email, we will get the following output − Print Page Previous Next Advertisements ”;

iOS – Delegates

iOS – Delegates ”; Previous Next Example for Delegate Let”s assume an object A calls an object B to perform an action. Once the action is complete, object A should know that B has completed the task and take necessary action. This is achieved with the help of delegates. The key concepts in the above example are − A is a delegate object of B. B will have a reference of A. A will implement the delegate methods of B. B will notify A through the delegate methods. Steps in Creating a Delegate Step 1 − First, create a single view application. Step 2 − Then select File → New → File… Step 3 − Then select Objective C Class and click Next. Step 4 − Give a name to the class, say, SampleProtocol with subclass as NSObject as shown below. Step 5 − Then select create. Step 6 − Add a protocol to the SampleProtocol.h file and the updated code is as follows − #import <Foundation/Foundation.h> // Protocol definition starts here @protocol SampleProtocolDelegate <NSObject> @required – (void) processCompleted; @end // Protocol Definition ends here @interface SampleProtocol : NSObject { // Delegate to respond back id <SampleProtocolDelegate> _delegate; } @property (nonatomic,strong) id delegate; -(void)startSampleProcess; // Instance method @end Step 7 − Implement the instance method by updating the SampleProtocol.m file as shown below. #import “SampleProtocol.h” @implementation SampleProtocol -(void)startSampleProcess { [NSTimer scheduledTimerWithTimeInterval:3.0 target:self.delegate selector:@selector(processCompleted) userInfo:nil repeats:NO]; } @end Step 8 − Add a UILabel in the ViewController.xib by dragging the label from the object library to UIView as shown below. Step 9 − Create an IBOutlet for the label and name it as myLabel and update the code as follows to adopt SampleProtocolDelegate in ViewController.h. #import <UIKit/UIKit.h> #import “SampleProtocol.h” @interface ViewController : UIViewController<SampleProtocolDelegate> { IBOutlet UILabel *myLabel; } @end Step 10 Implement the delegate method, create object for SampleProtocol and call the startSampleProcess method. The Updated ViewController.m file is as follows − #import “ViewController.h” @interface ViewController () @end @implementation ViewController – (void)viewDidLoad { [super viewDidLoad]; SampleProtocol *sampleProtocol = [[SampleProtocol alloc]init]; sampleProtocol.delegate = self; [myLabel setText:@”Processing…”]; [sampleProtocol startSampleProcess]; // Do any additional setup after loading the view, typically from a nib. } – (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark – Sample protocol delegate -(void)processCompleted { [myLabel setText:@”Process Completed”]; } @end Step 11 We will see an output as follows. Initially the label displays “processing…”, which gets updated once the delegate method is called by the SampleProtocol object. Print Page Previous Next Advertisements ”;

iOS – Twitter & Facebook

iOS – Twitter and Facebook ”; Previous Next Twitter has been integrated in iOS 5.0 and Facebook has been integrated in iOS 6.0. Our tutorial focuses on using the classes provided by Apple and the deployment targets for Twitter and Facebook are iOS 5.0 and iOS 6.0 respectively. Steps Involved Step 1 − Create a simple view-based application. Step 2 − Select your project file, then select targets and then add Social.framework and Accounts.framework in choose frameworks. Step 3 − Add two buttons named facebookPost and twitterPost and create ibActions for them. Step 4 − Update ViewController.h as follows − #import <Social/Social.h> #import <Accounts/Accounts.h> #import <UIKit/UIKit.h> @interface ViewController : UIViewController -(IBAction)twitterPost:(id)sender; -(IBAction)facebookPost:(id)sender; @end Step 5 − Update ViewController.m as follows − #import “ViewController.h” @interface ViewController () @end @implementation ViewController – (void)viewDidLoad { [super viewDidLoad]; } – (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(IBAction)facebookPost:(id)sender { SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){ if (result == SLComposeViewControllerResultCancelled) { NSLog(@”Cancelled”); } else { NSLog(@”Done”); } [controller dismissViewControllerAnimated:YES completion:nil]; }; controller.completionHandler = myBlock; //Adding the Text to the facebook post value from iOS [controller setInitialText:@”My test post”]; //Adding the URL to the facebook post value from iOS [controller addURL:[NSURL URLWithString:@”http://www.test.com”]]; //Adding the Text to the facebook post value from iOS [self presentViewController:controller animated:YES completion:nil]; } -(IBAction)twitterPost:(id)sender { SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; [tweetSheet setInitialText:@”My test tweet”]; [self presentModalViewController:tweetSheet animated:YES]; } @end Output When we run the application and click facebookPost, we will get the following output − When we click twitterPost, we will get the following output − Print Page Previous Next Advertisements ”;

iOS – UI Elements

iOS – UI Elements ”; Previous Next What UI Elements are? UI elements are the visual elements that we can see in our applications. Some of these elements respond to user interactions such as buttons, text fields and others are informative such as images, labels. How to Add UI Elements? We can add UI elements both in code and with the help of interface builder. Depending on the need we can use either one of them. Our Focus We”ll be focussing more on adding UI elements through code in our applications. Using interface builder is simple and straight forward, we just need to drag and drop the UI elements. Our Approach We will create a simple iOS application and use it for explaining some of the UI elements. Step 1 − Create a Viewbased application as we did in our First iOS application. Step 2 − We will be only updating the ViewController.h and ViewController.m files. Step 3 − Then we add a method to our ViewController.m file specific for creating the UI element. Step 4 − We will call this method in our viewDidLoad method. Step 5 − The important lines of code have been explained in the code with single line comment above those lines. List of UI Elements UI specific elements and their related functionalities are explained below − Sr.No. UI Specific Elements 1 Text Fields It is an UI element that enables the app to get user input. 2 Input types – TextFields We can set the type of input that user can give by using the keyboard property of UITextField. 3 Buttons It is used for handling user actions. 4 Label It is used for displaying static content. 5 Toolbar It is used if we want to manipulate something based on our current view. 6 Status Bar It displays the key information of device. 7 Navigation Bar It contains the navigation buttons of a navigation controller, which is a stack of view controllers which can be pushed and popped. 8 Tab bar It is generally used to switch between various subtasks, views or models within the same view. 9 Image View It is used to display a simple image or sequence of images. 10 Scroll View It is used to display content that is more than the area of screen. 11 Table View It is used for displaying scrollable list of data in multiple rows and sections. 12 Split View It is used for displaying two panes with master pane controlling the information on detail pane. 13 Text View It is used for diplaying scrollable list of text information that is optionally editable. 14 View Transition It explains the various view transitions between views. 15 Pickers It is used for displaying for selecting a specific data from a list. 16 Switches It is used as disable and enable for actions. 17 Sliders It is used to allow users to make adjustments to a value or process throughout a range of allowed values. 18 Alerts It is used to give important information to users. 19 Icons It is an image representation used for an action or depict something related to the application. Print Page Previous Next Advertisements ”;