Showing posts with label iOS iPhone iPad Tutorial. Show all posts
Showing posts with label iOS iPhone iPad Tutorial. Show all posts

Thursday, 12 June 2014

iOS - Application Debugging

Create By: Osama Sheikh
(S.E) And I,T Expert
_________________________________
Post By: 
Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------

iOS - Application Debugging

Introduction

When we do application we may commit mistakes which can lead to different kinds of errors. So in order to fix these errors or bugs we need to debug the application.

Selecting a Debugger

Xcode has to debuggers namely GDB and LLDB debuggers, and GDB is selected by default. LLDB is a debugger that is part of the LLVM open source compiler project. You can change the debugger by edit active schemes option.

How to find coding mistakes?

We just need to build our application and our code is compiled by compiler and all the messages, errors and warnings with their reason will be displayed and we can correct them. You can click Product and click analyze, you will be given the possible issues in the application.

Set Breakpoints

Break points help us to know the different states of our application objects which help us identifying many flaws including logical issues. We just need to click over the line number to create a breakpoint. We can click and drag it out to remove the break point. An example breakpoint is shown below.
iOS Tutorial
When we run the application and select the playVideo button the application will be paused for us to analyze the states of our application. When breakpoint is triggered we will get an output as shown below.
iOS Tutorial
You can easily identify which thread has triggered the breakpoint. In the bottom you can see objects like self, sender and so on which hold the values of corresponding objects and we can expand some of these objects and see what is the state of each of them.
To continue the application we will select the continue button (left most button) in the debug area shown below. The other options include step in, step out and step over.
iOS Tutorial

Exception breakpoint

We also have exception breakpoint that triggers application to stop in location where the exception occurs. We can create exception breakpoint by selecting the + button after selecting the debug navigator. You will get the following window.
iOS Tutorial
Then we need to select Add Exception Breakpoint which will show the following window.
iOS Tutorial


Posted By MIrza Abdul Hannan7:23:00 am

iOS - Memory Management

Create By: Osama Sheikh
(S.E) And I,T Expert
_________________________________
Post By: 
Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------

iOS - Memory Management

Introduction

Memory management in iOS was initially non ARC(Automatic reference counting) where we have to retain and release the objects. Now it supports ARC where we don't need to add retain and release. Actually the Xcode takes care of the job automatically in compile time.

Problems faced

The two major problems faced as per apple documentation are,
  • Freeing or overwriting data that is still in use. This causes memory corruption, and typically results in your application crashing, or worse, corrupted user data.
  • Not freeing data that is no longer in use causes memory leaks. A memory leak is where allocated memory is not freed, even though it is never used again. Leaks cause your application to use ever-increasing amounts of memory, which in turn may result in poor system performance or (in iOS) your application being terminated.

Memory Management rules

  • We own the objects we create and we have to subsequently release them when they are no longer needed.
  • Retain can be used t gain ownership of an object that we did not create. We have release these objects too when it's not needed.
  • Don't release the objects that we don't own.

Handling memory in ARC

You don't need to use release and retain in ARC. So, all the view controller's objects will be released when the view controller is removed. Similarly any objects sub objects will be released when they are released. Remember if other classes have strong reference to an object of the class then the whole class won't be released. So it is recommended to use weak properties for delegates.

Using memory management tools

We can analyze the usage of memory with the help of Xcode tool Instruments. It includes tools like activity monitor, Allocations, leaks, zombies and so on.

Steps for analyzing memory allocations

1. Open an existing application.
2. Select Product and then Profile as shown below.
iOS Tutorial
3. Then select Allocations in the next screen shown below and select Profile.
iOS Tutorial
4. We will see the allocation of memory for different objects as shown below.
5. You can switch between view controllers and check whether the memory is released properly.
iOS Tutorial
6. Similarly instead of Allocations we can use Activity Monitor to see the overall memory allocated for the application and it's shown below.
iOS Tutorial
7. These tools help in knowing how much we use and what the places possible leaks have occurred

Posted By MIrza Abdul Hannan7:20:00 am

iOS - Twitter & Facebook

Create By: Osama Sheikh
(S.E) And I,T Expert
_________________________________
Post By: 
Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------

iOS - Twitter & Facebook

Introduction

Twitter has been integrated to 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

1. Create a simple View based application.
2. Select your project file, then select targets and then add Social.framework andAccounts.framework in choose frameworks
3. Add two buttons named facebookPost , twitterPost and create ibActions for them.
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
4. 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

Now when we run the application and click facebookPost we will get following output.
iOS Tutorial
When we click twitterPost we will get following output.
iOS Tutorial

Posted By MIrza Abdul Hannan7:19:00 am

iOS - Auto Layouts


Create By: Osama Sheikh
(S.E) And I,T Expert
_________________________________
Post By: 
Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------
iOS - Auto Layouts

Introduction

Auto layouts were introduced in iOS 6.0 and we can support only deployment target of 6.0 and higher. It helps us create interfaces that can be used for multiple orientations and multiple devices.

Goal of our example

We will add two buttons that will be placed a certain distance from the center of screen. We will also try to add a resizable text field that will be placed from a certain distance from above the buttons.

Our Approach

We will add text field and two buttons in code along with their constraints. The constraints of each UI Elements will be created and added to the super view and we will have to disable auto resizing for each of the UI elements we add in order get the desired result.

Steps Involved

1. Create a simple View based application.
2. We will only edit ViewController.m and it is as follows.
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, strong) UITextField *textfield;

@end
@implementation ViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    UIView *superview = self.view;
    /*1. Create leftButton and add to our view*/
    self.leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.leftButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.leftButton setTitle:@"LeftButton" forState:UIControlStateNormal];
    [self.view addSubview:self.leftButton];    
    /* 2. Constraint to position LeftButton's X*/
    NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint 
    constraintWithItem:self.leftButton attribute:NSLayoutAttributeCenterX 
    relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
    NSLayoutAttributeCenterX multiplier:1.0 constant:-60.0f];
    /* 3. Constraint to position LeftButton's Y*/
    NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint 
    constraintWithItem:self.leftButton attribute:NSLayoutAttributeCenterY 
    relatedBy:NSLayoutRelationEqual toItem:superview attribute:
    NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];   
    /* 4. Add the constraints to button's superview*/
    [superview addConstraints:@[ leftButtonXConstraint,
    leftButtonYConstraint]];    
    /*5. Create rightButton and add to our view*/
    self.rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.rightButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.rightButton setTitle:@"RightButton" forState:UIControlStateNormal];
    [self.view addSubview:self.rightButton];    
    /*6. Constraint to position RightButton's X*/
    NSLayoutConstraint *rightButtonXConstraint = [NSLayoutConstraint 
    constraintWithItem:self.rightButton attribute:NSLayoutAttributeCenterX 
    relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
    NSLayoutAttributeCenterX multiplier:1.0 constant:60.0f];
    /*7. Constraint to position RightButton's Y*/
    rightButtonXConstraint.priority = UILayoutPriorityDefaultHigh;
    NSLayoutConstraint *centerYMyConstraint = [NSLayoutConstraint 
    constraintWithItem:self.rightButton attribute:NSLayoutAttributeCenterY 
    relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
    NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];
    [superview addConstraints:@[centerYMyConstraint,
    rightButtonXConstraint]];
   //8. Add Text field
    self.textfield = [[UITextField alloc]initWithFrame:
    CGRectMake(0, 100, 100, 30)];
    self.textfield.borderStyle = UITextBorderStyleRoundedRect;
    self.textfield.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.textfield];
    //9. Text field Constraints
    NSLayoutConstraint *textFieldTopConstraint = [NSLayoutConstraint 
    constraintWithItem:self.textfield attribute:NSLayoutAttributeTop 
    relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview 
    attribute:NSLayoutAttributeTop multiplier:1.0 constant:60.0f];
    NSLayoutConstraint *textFieldBottomConstraint = [NSLayoutConstraint 
    constraintWithItem:self.textfield attribute:NSLayoutAttributeTop 
    relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.rightButton 
    attribute:NSLayoutAttributeTop multiplier:0.8 constant:-60.0f];
    NSLayoutConstraint *textFieldLeftConstraint = [NSLayoutConstraint 
    constraintWithItem:self.textfield attribute:NSLayoutAttributeLeft 
    relatedBy:NSLayoutRelationEqual toItem:superview attribute:
    NSLayoutAttributeLeft multiplier:1.0 constant:30.0f];
    NSLayoutConstraint *textFieldRightConstraint = [NSLayoutConstraint 
    constraintWithItem:self.textfield attribute:NSLayoutAttributeRight 
    relatedBy:NSLayoutRelationEqual toItem:superview attribute:
    NSLayoutAttributeRight multiplier:1.0 constant:-30.0f];
    [superview addConstraints:@[textFieldBottomConstraint ,
    textFieldLeftConstraint, textFieldRightConstraint, 
    textFieldTopConstraint]];   
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

What we did here?

In steps marked 1, 5 and 8 we just programmatically add two buttons and a text field respectively. In rest of the steps we create constraints and add those constraints to respective super views which are actually self.view. When you look into the one of the left button's constraint as shown below,
    NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint 
    constraintWithItem:self.leftButton attribute:NSLayoutAttributeCenterX 
    relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
    NSLayoutAttributeCenterX multiplier:1.0 constant:-60.0f];
We actually have constraintWithItem and toItem which decides between which UI element we are creating the constraint. The attribute decides on what basis the two elements have linked together. Related by decides how much the attributes have effect between the elements. Multiplier is the multiplication factor and constant will be added to the multipler. Here we are saying that X of leftButton is always greater than or equal to -60 pixels with respect to the center of the super view. Similarly other constraints are defined.

Output

Now when we run the application we'll get the following output on iPhone simulator.
iOS Tutorial
When we change the orientation of the simulator to landscape we will get following output.
iOS Tutorial
When we run the same application on iPhone 5 simulator we will get the following output.
iOS Tutorial
When we change the orientation of the simulator to landscape we will get following output.
iOS Tutorial

Posted By MIrza Abdul Hannan6:57:00 am

iOS - Storyboards

Create By: Osama Sheikh
(S.E) And I,T Expert
_________________________________
Post By: 
Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------

iOS - Storyboards

Introduction

Storyboards are introduced in iOS 5 and when we use storyboards our deployment target should be 5.0 or higher. Storyboards helps us visual create all the screens of the application along with their flow of screens under one interface MainStoryboard.storyboard. It also helps in reducing coding of pushing/presenting view controllers.

Steps Involved

1. Create a single view application and make sure that you select storyboard checkbox while creating the application.
2. Select MainStoryboard.storyboard where you can find single view controller. Now add one more view controller and update the view controllers as shown below.
iOS Tutorial
3. Now let connect both the view controllers. For this right click on the button name "show modal" and drag it to the right view controller in the left side view controller as shown below.
iOS Tutorial
4. Now select modal from the three options displayed as shown below.
iOS Tutorial
5. Update ViewController.h as follows.
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

-(IBAction)done:(UIStoryboardSegue *)seque;

@end
6. 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)done:(UIStoryboardSegue *)seque{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
7. Select MainStoryboard.storyboard and right click on the Exit button in the right side view controller select done and connect with back button as shown below.
iOS Tutorial

Output

Now when we run the application in iPhone device we'll get the following output.
iOS Tutorial
Now when we select show modal we will get the following output.
iOS Tutorial

Posted By MIrza Abdul Hannan6:54:00 am