• Skip to main content
  • Skip to primary sidebar

Ravi Shankar

Tweaking Apps

  • Swift
  • Tech Tips

iOS

Attribute Unavailable: Estimated section warning before iOS 11.0

November 12, 2021 By Ravi Shankar

Xcode 13 (iOS 15) displays the following warning message for project having minimum deployment as iOS 10.

  • Attribute Unavailable: Estimated section header height before iOS 11.0
  • Attribute Unavailable: Estimated section footer height before iOS 11.0

This warning gets displayed for new UITableViewController added to the Storyboard file. You can get rid of the warning messages by adding 0 to Estimate Header and Estimate footer for Sections under Size Inspector

Filed Under: ios, Swift, Xcode Tagged With: iOS, Xcode Warning

Basic overview of Xcode

June 21, 2014 By Ravi Shankar Leave a Comment

Xcode is the primary tool used for the development of Mac and iOS applications. This is a free tool which can be downloaded from developer.apple.com website. You can use Xcode for Writing code, Building, Testing (Unit test) and for Distribution (Submitting to App Store).

Xcode Window

Different Panes in Xcode

Navigator

 

This is available at the left hand side of Xcode window. Provides option to navigate through the project files, Issues, Debug Session, Breakpoints etc.

Xcode Navigator Pane

Utilities

 

This is available at the right hand side of Xcode window. This contains File inspector, Quick Help inspector, File Template Library, Code Snippet Library, Object library and Media library. These tools help the developer with designing the user interface and writing coding using Xcode.

Xcode Utilities Pane

Editor

This available at the Centre of Xcode window and used for writing your code.

Xcode Editor Window

Interface builder

Interface builder or IB is available at the Centre of Xcode window when you navigate to your .xib file or storyboard. This allows users to build UI for their app.

Xcode Interface Builder

Debug and Console

 

Debug and Console panes are available at the bottom of Xcode window. As the name suggests Debug provides option for debugging your app and console displays both system, exceptions and app written messages.

Xcode Debug and Console Windows

Toolbar

Xcode Toolbar

The Toolbar provides the option to Run, Analyse, Build and Test your App. This is the place where you would specify whether you want to run the app in simulator (iPhone or iPad) or real device,

Xcode Choose Simulator

Another important feature of Toolbar is the option it provides to show or hide panes of Xcode.

Xcode Show or Hide Pane

The Editor option provides toggle option to show or hide Standard Editor, Assistant Editor and Version Editor. Similarly View option provide option to show or hide Navigator, Debug Area and Utilities. You can also launch Organizer from Xcode Toolbar.

Oragnizer

This provides access to the Documentation, Projects, Archives, Repositories and Devices (Provisioning Profiles)

Xcode Organizer

Filed Under: Develop, Xcode Tagged With: iOS, iOS Simulator

Test Driven Development in iOS – Beginners tutorial – Part 1

April 13, 2014 By Ravi Shankar 7 Comments

This is a beginner tutorial on TDD in iOS, explained with an example app that adds two numbers. Let us see try to use some XCTest assertions supported in iOS in this project.

Create a New iOS project select the template as Empty Application

201404132059.jpg

Enter the required details in Choose options for your new project screen.

201404132104.jpg

Step 3: Then specify the folder to save this project.

201404132106.jpg

The project navigator screen will have two targets, one for app and add another for XCTest framework. The folder that ends with “Tests” will contain the default XCTest file .

Lets use the class AddingTwoNumbersTests.m for testing the application logic. Open AddingTwoNumbersTests.m and delete default testExample method.

201404132127.jpg

Create a failing method that tests for existence of new “RSAddition” class that is going to have method for adding two numbers.

201404132155.jpg  

You should notice the errors displayed in the above screenshot after adding testAdditionClassExists. To fix these error, create a new class named RSAddition subclass of NSObject and add the class to both the targets. Then import the Addition.h in “AddingTwoNumbersTests.m”.

201404132152.jpg

201404132153.jpg

Now the tests should pass when it gets executed. You should notice the green tick mark before the class and method and shown in the below screenshot.

201404132158.jpg

Now add the following method to do a simple addition of 2+2.

-(void)testAddTwoPlusTwo {

RSAddition *addition = [[RSAddition alloc] init];

NSInteger result = [addition addNumberOne:2 withNumberTwo:2];

XCTAssertEqual(result, 4, @”Addition of 2 + 2 is 4″);

}

RSAddition class needs to have a new method addNumberOne: withNumberTwo: Add the method definition in RSAddition.h interface file.

-(NSInteger)addNumberOne:(NSInteger) firstNumber withNumberTwo:(NSInteger) secondNumber;

Then add the following method to RSAddition.m implementation file. Let us hardcode the result “4” for time being to pass this test.

-(NSInteger)addNumberOne:(NSInteger) firstNumber withNumberTwo:(NSInteger) secondNumber {

return 4;

}

Now the test should get executed successfully.
201404132209.jpg
Let us add another test to fix the hardcoding problem. This time we will add two different numbers 2 and 7. But before adding the test method, we need to refactor the existing code in the test file. The below code is used by both the test methods and this is the best candidate to be placed under setup method.

  RSAddition *addition = [[RSAddition alloc] init];

The changed code should look as shown in the below screenshot.

201404132223.jpg

Adding the below method should fail as the addition method is hardcoded to return value as 4.

-(void)testAddTwoPlusSeven {

NSInteger result = [addition addNumberOne:2 withNumberTwo:7];

XCTAssertEqual(result, 9, @”Addition of 2 + 7 is 9″);

}

201404132226.jpg

Now edit the addition method to fix this problem. It requires just a one line change and now the test should pass.

201404132229.jpg

201404132231.jpg

The next tutorial will cover the steps for adding the view controller and required fields for adding two numbers using the above added application logic class.

Download source code from here.

Filed Under: ios, iPhone, Xcode Tagged With: Assertions, iOS, TDD, Xcode, XCTest

Quickly copy photos in to photos album in iOS Simulator

January 10, 2014 By Ravi Shankar Leave a Comment

Photos Album in iOS Simulator is generally empty and if you are writing apps using Photos functionality then you might need some photos in them. Listed below are the steps for placing photos under the Photos Album in Simulator.

Step 1: Access the Home screen on iOS simulator. (Hardware > Home)

Access Home Screen on iOS Simulator

If you tap Photos then you should see “No Photos or Videos” message.

No Photos or Videos

Step 2: Now tap Safari icon on your Home screen.

Step 3: Open Finder window, locate the image that you want to copy then drag and drop the image on to Safari browser.

Step 4: Right click and hold for some time until you see the action sheet with an option to Save Image.

Save Image on Simulator

Navigate back to Home screen, tap the Photos option and this should show display the recently added Photo.

Photo Album on Simulator with Photos

Filed Under: ios, iPhone, Xcode Tagged With: iOS, iOS Simulator, Photos Album, Xcode

Simple UITableView and UIAlertView example

January 9, 2014 By Ravi Shankar 1 Comment

In this article, we are going to see how to create a simple UITableView for displaying data. And display an alert on selection of any row using UIAlertView.

Launch Xcode, Click File > New Project and select Single View Application as the project template.

Choose a template for new new project

Enter the project details for Product Name, Organization Name, Company Identifier and Target device as iPhone.

201401091129.jpg

Then choose a location on your Mac to save the project.

201401091131.jpg

Navigate to Main.Storyboard and and delete the ViewController under View Controller Scene. Since we want a TableView, we are going to replace this View Controller with UITableViewController.

201401091134.jpg

Note :- We could have also used UITableView on top of View Controller but we will keep that for another session.

Now select UITableViewController from the Object library, drag and drop it on to the User Interface.

201401091141.jpg

Let us see the how to display list of values in the above table.

Navigate to ViewController.h in the Project Navigator and replace the following line

@interface ViewController : UIViewController

with

@interface ViewController : UITableViewController

Then use the Identity Inspector and specify the class for TableViewController as ViewController.

201401091146.jpg

Edit ViewController.m file and add a new instance variable and populate the cities in ViewDidLoad method.

@implementation ViewController

{

NSArray *cities;

}

– (void)viewDidLoad

{

[super viewDidLoad];

  

//Create the list of cities that will be displayed in the tableview

  

cities = [[NSArray alloc] initWithObjects:@”Chennai”,@”Mumbai”,@”New Delhi”, @”New York”, @”London”,@”Tokyo”,@”Stockholm”,@”Copenhagen”,@”Manchester”,@”Paris”,nil];

}

In the above code, cities is of type NSArray and it is initialised with set of values in the ViewDIdLoad method. These values will be displayed in the UITableView.

The ViewController will act as a delegate and datasource for the UITableViewController. These connections are automatically created and you can verify them by right clicking on the ViewController. If we had used UITableView instead of UITableViewController then these connections have to be made manually.

201401091202.jpg

The data for the tableview is provided by the following methods and these methods needs to be implemented in ViewController.m.

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

numberOfRowsInSection method is used for specifying the total rows in each section for the tableview. In this example, the UITableView has only one section for displaying the cities.

cellForRowAtIndexPath method is used for providing the data to each row in the TableView. The data is populated using the indexPath for each row.

Copy the method implementation to ViewController.m file

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return [cities count];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@”Cities”];

  

cell.textLabel.text = [cities objectAtIndex:indexPath.row];

  

return cell;

}

The numberOfRowsInSection provides the tableview with the total count of rows i.e number of items in the cities array object. In the cellForRowAtIndexPath, we create a reusable UITableViewCell and assign the cities as per the row indexPath. Also make sure to specify the Identifier for prototype cell (User Interface) as Cities using the Attributes Inspector.

Now if you build and run the project, you should see a table with list of cities as shown below.

201401091307.jpg

Display the row selection using UIAlertView

Nothing will happen when you select any of the rows in the above table. Let us use the UIAlertView to display the row selection and for this we need to implement didSelectRowAtIndexPath in ViewController.m. Add the following didSelectRowAtIndexPath method implementation to the file.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

NSString *cityName = [cities objectAtIndex:indexPath.row];

  

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@”City” message:cityName delegate:self cancelButtonTitle:@”Cancel” otherButtonTitles:nil, nil];

  

[alertView show];

}

The above code first retrieves the city name from cities array object using the row’s indexPath. In the second line, we create a instance of UIAlertView with title as “City”, selected city for the message attribute and provide the title for the cancel button. The delegate for UIAlertView will be ViewController and this specified by the delegate attribute. In the last line we use the show method to display the alert.

UITableView showing UIAlertView

Download the source code from here.

Filed Under: Develop, ios, iPhone, Programming, Xcode Tagged With: iOS, iphone, UIAlertView, UITableView, Xcode

How to add annotation to MapView in iOS

January 4, 2014 By Ravi Shankar 12 Comments

This article provides the steps for adding annotation to a MapView in iOS

Updated: – Click here for the updated article in Swift

Create a new Xcode Project – File > New > Project. Select the template for the project as Singe View Application under iOS > Application.

201401041109.jpg

Then provide the required details in the options for your new project screen.

201401041101.jpg

Select Main.storyboard file in the Navigator Area. This should display the User Interface in the Editor Area. Now select Map View from the Object Library, drag and drop it on the User Interface. Adjust the width and height of MKMapView to cover the ViewController.

201401041112.jpg

Now is if you try to Build and Run the project on iOS simulator, you will see the following errors message on the console window.

‘NSInvalidUnarchiveOperationException’, reason: ‘Could not instantiate class named MKMapView‘

This occurs when the MapKit library is not as part of the Link Binary with Libraries under Build Phases.

201401041121.jpg

Now add annotation, let us create a plist file containing array of locations with title, latitude and longitude.

Right click on the Project under the Navigator Area and select New File option.

201401041124.jpg

In the template for New File, select Property List under iOS > Resource section.

201401041127.jpg

Now provide name for the plist and save it under the Project directory.

201401041128.jpg

Now change the plist Root type to Array and start adding items of type Dictionary.

201401041129.jpg

The Dictionary item will contain title, latitude and longitude. As shown below, there are 6 items added to plist file.

201401041449.jpg

Create MapViewAnnotation class

In order display the location as annotation, we need to create custom annotation implementing the interface MKAnnotation.

201401041524.jpg

Create a MapViewAnnotation class subclassing NSObject

201401041526.jpg

Navigate to MapViewAnnotation.h header file and replace the content with the following

#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

@interface MapViewAnnotation : NSObject <MKAnnotation>

@property (nonatomic,copy) NSString *title;

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

-(id) initWithTitle:(NSString *) title AndCoordinate:(CLLocationCoordinate2D)coordinate;

@end

Now navigate to MapViewAnnotation.m header file and replace the content with the following.

#import “MapViewAnnotation.h”

@implementation MapViewAnnotation

@synthesize coordinate=_coordinate;

@synthesize title=_title;

-(id) initWithTitle:(NSString *) title AndCoordinate:(CLLocationCoordinate2D)coordinate

{

self = [super init];

_title = title;

_coordinate = coordinate;

return self;

}

@end

 

The above code creates a title and coordinate property and these properties are initialised in the implementation class using the initWithTitle:(NSString *) title AndCoordinate:(CLLocationCoordinate2D)coordinate method.
Creating and adding annotation to the MapView
Create a IBOutlet property to MKMapView and connect the outlet the MapView in the User Interface. After adding the IBOutlet, the ViewController should look like this

#import <UIKit/UIKit.h>

#import <MapKit/MapKit.h>

@interface ViewController : UIViewController

@property (nonatomic,strong) IBOutlet MKMapView *mapview;

@end

Navigate to ViewController.m and add new method createAnnotations for creating the annotation from the plist and loading the annotation to the map view.

– (NSMutableArray *)createAnnotations

{

NSMutableArray *annotations = [[NSMutableArray alloc] init];

//Read locations details from plist

NSString *path = [[NSBundle mainBundle] pathForResource:@”locations” ofType:@”plist”];

NSArray *locations = [NSArray arrayWithContentsOfFile:path];

for (NSDictionary *row in locations) {

NSNumber *latitude = [row objectForKey:@”latitude”];

NSNumber *longitude = [row objectForKey:@”longitude”];

NSString *title = [row objectForKey:@”title”];

//Create coordinates from the latitude and longitude values

CLLocationCoordinate2D coord;

coord.latitude = latitude.doubleValue;

coord.longitude = longitude.doubleValue;

MapViewAnnotation *annotation = [[MapViewAnnotation alloc] initWithTitle:title AndCoordinate:coord];

[annotations addObject:annotation];

}

return annotations;

}

In the above method, we read the locations from the plist file. Then create CLLocationCoordinate2D for each location using the latitude and longitude details. These details are then used for creating the MapViewAnnotation object.
In the ViewDidLoad method, load the annotations to the MapView by calling createAnnotations method.

– (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

[self.mapview addAnnotations:[self createAnnotations]];

}

If you try to build and run the project, these annotations will not be displayed as the map is not zoomed to these locations. You can fix by adding the following zoomToLocation method.

– (void)zoomToLocation

{

CLLocationCoordinate2D zoomLocation;

zoomLocation.latitude = 13.03297;

zoomLocation.longitude= 80.26518;

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 7.5*METERS_PER_MILE,7.5*METERS_PER_MILE);

[self.mapview setRegion:viewRegion animated:YES];

[self.mapview regionThatFits:viewRegion];

}

The above code, creates coordinate for the zoom location. Then we define MKCoordinateRegion and set that as the region for the map view. The METERS_PER_MILE is a constant with the value as 1609.344 which needs to be added after the @implementation ViewController

#define METERS_PER_MILE 1609.344

Now you be able to compile and run the project and you should be able to see the following in iOS simulator. And selecting any annotation, should be display the title.

201401041644.jpg

201401041645.jpg

 

Download the source from here

Filed Under: ios, Xcode Tagged With: Annotations, iOS, MapKit.framework, MKMapView, Xcode

Objective-C – What are Categories?

January 29, 2013 By Ravi Shankar Leave a Comment

Categories in Objective-C are used for adding extra functionality to a class without accessing the source code of the class and without subclassing it. Let us see this with an example by adding an additional method to NSNumber that just writes the value of the NSNumber argument to NSLog.

Create Project

Create a new Command Line Tool project for the example by providing the required details and selecting Type as Foundation.

201301282235.jpg

201301282236.jpg

Add Objective-C Category

Right click on the project, select New File then choose template for your new file as Objective-C category.

201301282251.jpg

Provide the name for your Category and select Category On as NSNumber (The class for which we are adding the additional method)

201301290550.jpg

Write Implementation

Two new files NSNumber+PrintNumber.h and NSNumber+PrintNumber.m files will be added to your project. Define the new method for NSNumber in the header file as shown below.

@interface NSNumber (PrintNumber)

-(void) printNumber: (NSNumber *) num;

@end

where printNumber is the new method that takes num of type NSNumber as the argument.

In the implementation file (NSNumber+PrintNumber.m ), add the code that write the value number to NSLog.

-(void) printNumber: (NSNumber *) num

{

NSLog(@” The number value is %@”, num);

}

Using Categories

Now in main.m file make a call to the new method of NSNumber after importing the “NSNumber+PrintNumber.h” as shown below.

#import <Foundation/Foundation.h>

#import “NSNumber+PrintNumber.h”

int main(int argc, const char * argv[])

{

@autoreleasepool {

  

// insert code here…

NSLog(@”Hello, World!”);

NSNumber *num = [[NSNumber alloc ] initWithInt:25];

[num printNumber:num];

}

return 0;

}

The call to [num printNumber:num] will write the value to the console.

201301290635.jpg

– Categories have access to instance variable of the original class but cannot have its own instance variable.

– Categories on parent class will be available for all its subclass as well.

– Using Categories you can override another method in the class.

–

Filed Under: Develop, Programming Tagged With: iOS, Objective C

Primary Sidebar

TwitterLinkedin

Recent Posts

  • How to block keywords in Jio broadband
  • How to disable opening an app automatically at login in Mac
  • How to set preferred Wifi network on Mac
  • Attribute Unavailable: Estimated section warning before iOS 11.0
  • How to recover Firefox password from Time Machine backup

Pages

  • About
  • Privacy Policy
  • Terms and Conditions

Copyright 2022 © rshankar.com

Terms and Conditions - Privacy Policy