Thursday, 18 August 2016

How to create a Stepper in iOS using Objective C

Today we will learn how to create a Stepper in iOS.

Steps:

1. Create a new Project Lets say (StepperDemo).
2. Now go to StoryBoard and take stepper and a label from Object Library.



3. Now create property and ValueChanged Action for Stepper.

@property (weak, nonatomic) IBOutlet UILabel *stepperValueLabel;
@property (weak, nonatomic) IBOutlet UIStepper *stepper;
- (IBAction)valueChanged:(UIStepper *)sender {

   }


4.  Now go to ViewDidLoad and set the maximum and minimum value of Stepper.

   - (void)viewDidLoad {
                [super viewDidLoad];
                _stepper.minimumValue = 1;
                _stepper.maximumValue = 10;
                // Do any additional setup after loading the view, typically from a nib.
        }


5. Now write Button Action .

 - (IBAction)valueChanged:(UIStepper *)sender {
            double value = [sender value];
            [_stepperValueLabel setText:[NSString stringWithFormat:@"%d", (int)value]];
        }

Now run the code and see the desired output.


Please do share Your comment and reviews ..
You can download the whole source code from here.

Enjoy Coding :)

Sunday, 14 August 2016

Celebrating indian independence day

Download this beautiful App on the eve of  Independence day.

App Store Link


Draw route between two places and finding distance between them in MKMapView

Today We will learn how to draw path between two places without using GoogleAPI and just using the methods of MKMapView.
Full Code And Explanation Here.

How to change Screen with UIPageController in iOS.

Today we will learn how to change Screen with UIPage Controller in iOS.

Steps:

1. Create a new Xcode Project (Lets say UIPageControlDemo).
2. Now go to StoryBoard and take one label and one PageControl object.


3. Make proper IBOutlet Connection and then go to Your ViewController.m and write the following code.


#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *pageNoLabel;
@property (weak, nonatomic) IBOutlet UIPageControl *pageController;

@end

@implementation ViewController
{
    NSTimer *timer;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _pageController.currentPage = 0;
    _pageController.numberOfPages = 11;
    timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(onTick:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)onTick:(NSTimer *)timer1 {
    if (_pageController.currentPage == 10) {
        [timer invalidate];
    }
    else{
        _pageController.currentPage = _pageController.currentPage+1;
        _pageNoLabel.text = [NSString stringWithFormat:@"%ld",(long)_pageController.currentPage];
    }

}

4. Now run the code and see the desired ouput.


  Do customize it according to your own need.  Please share your reviews and comments.
You can download the whole Source code from here.

 Enjoy Coding :)

Thursday, 11 August 2016

How to get a list of all Contacts in iOS using Contacts framework.

Today we will learn how to get list of all contact from iOS device using Contacts framework. Earlier We used to use AddressBook framework, but this is now deprecated in iOS 9. So we will use the latest Contacts framework.

Full Code And Explanation here.

Wednesday, 10 August 2016

AutoPlace completion using MKLocalSearchCompleter in iOS using Objective C

We have seen Google API for autocompletion of places . But in iOS 9.3 apple introduced MKLocalSearchCompleter .

Today we will learn how to use this to search places nearby our Location.

Steps:
1. create a new Xcode Project Lets say MKLocalSearchControllerDemo.
2. Now go to storyBoard and take one searchbar, tableview and one mapView.

3. Make Proper IBOutLet Connection and add MapKit Framework in your project.

#import "ViewController.h"
#import <Mapkit/MapKit.h>

@interface ViewController ()<UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;




4. Now go to YourView ViewDidLoad and write this code.

@end
@implementation ViewController
{
    MKLocalSearch *localSearch;
    MKLocalSearchResponse *results;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    CLLocationCoordinate2D india = CLLocationCoordinate2DMake(17.4622,78.3568);
    [self.mapView setRegion: MKCoordinateRegionMakeWithDistance(india, 2000, 2000)];
    [self.mapView setMapType: MKMapTypeStandard];
    
    // Do any additional setup after loading the view, typically from a nib.

}

Note. Here I am fixing the coordinate to my place. But You can use CLLocationManager to get the current location of the user.
I am also fixing the radius in which it will search, you can increase decrease according to your need.

5. Now just write the following code .

#pragma mark -  tableView Delagate Methods


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [results.mapItems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *IDENTIFIER = @"SearchResultsCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:IDENTIFIER];
    }
    
    MKMapItem *item = results.mapItems[indexPath.row];
    
    cell.textLabel.text =  cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",item.name,item.placemark.addressDictionary[@"Street"]];
   // cell.detailTextLabel.text = item.placemark.addressDictionary[@"Street"];
    
    return cell;
}



#pragma mark - Search delegate methods

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = searchText;
    request.region = self.mapView.region;
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    localSearch = [[MKLocalSearch alloc] initWithRequest:request];
    
    [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){
        
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
        if (error != nil) {
          return;
        }
        
        if ([response.mapItems count] == 0) {
          
            return;
        }
        for (MKMapItem *item in response.mapItems)
        {
            MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
            annotation.coordinate = item.placemark.coordinate;
            annotation.title      = item.name;
            annotation.subtitle   = item.placemark.title;
            [self.mapView addAnnotation:annotation];
        }
        results = response;
        [_tableView reloadData];
        
        
    }];
}

6. Now run the code and see the output. It will show you the places name and it will show draw annotation in tha mapView.




You can share your comment and reviews if you liked it.

Enjoy Coding :)

Create Alert Controller with textfield in iOS

Today we will see how to give textfield in AlertController. And we will put condition that if users do not fills anything in the textfield, we will not dismiss it.

Steps:
1.  Create a new Xcode Project.
2. Now go to storyboard and take a button and make proper IBOutlet connection for it.
3. Now write this code in Your ViewController.m

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate>
@property(nonatomic, strong)UIAlertAction *okayAction;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)showAlertButtonAction:(id)sender {
    
    self.okayAction = [UIAlertAction actionWithTitle:@"Okay"
                                             style:UIAlertActionStyleDefault
                                           handler:nil];
    self.okayAction.enabled = NO;
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                        message:@"Please Enter your text"
                                                                 preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        
        textField.delegate = self;
    }];
    
    [alertController addAction:self.okayAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSString *textFieldString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    [self.okayAction setEnabled:(textFieldString.length >= 1)];
    return YES;
}

4. Now run the  code. And click the button. You will get the desired output.

Friday, 5 August 2016

How to implement WKWebView in iOS

In iOS 8, Apple introduced WKWenView which is more power and faster than the UIWebView.
Today We will learn how to create a Simple WKWebView  in iOS.

Thursday, 4 August 2016

A Simple Collection View Example in iOS using Objective C.

Today we will learn how to create a simple collection View example in iOS.

Steps:
1. Create a new Xcode Project Lets Say(CollectionViewDemo).
2. Now go to File-New-File and create a new CocoaTouchClass. and give it name MyCollectionViewCell.


3. Now go tho storyBoard and drag and drop a collection view from object Library.
Make IBOutlet connection for collectionView. Add one image in your project. I have
added ball.png in my project.

4. Now change class of cell to MyCollectionViewCell.


5.  Now put a imageView inside the cell.


5. Now give the MyCollectionViewCell identifier name as "Cell".


6. Now make the connection for the cell imageView.

7.Now go to your ViewController.m and write the following code.

#import "ViewController.h"
#import "MyCollectionViewCell.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 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 - collectionView Delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 12;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.cellImagView.image = [UIImage imageNamed:@"Ball.png"];
    
    return cell;
}

 8.  Now run the code and see the desired output.


You can download the whole Project from here.
Please share Your reviews.
Enjoy Coding :)


Wednesday, 3 August 2016

Implement Pull to refresh in UITableView in iOS using Objective C

Today we will learn how to give pull to refresh in tableView in iOS.

Steps:
1. Create a new Xcode Project Lets say (PullToRefreshDemoTableView).
2.  Now go to StoryBoard and take a TableView and connect properties and delegate Methods.

3. Now go to Your View Controller.m and write the following code in Your ViewDidLoad.
#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController{
     NSMutableArray *tableDataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    tableDataArray = [[NSMutableArray alloc]initWithObjects:@"Ram",@"Shyam",@"Mohan",@"nilesh", nil];
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
    [self.tableView addSubview:refreshControl];
    // Do any additional setup after loading the view, typically from a nib.
}
4. Now Write the dataSource and delgate method of tableView.

#pragma mark -  tableView Delagate Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [tableDataArray count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
                     }
    cell.textLabel.text = [tableDataArray objectAtIndex:indexPath.row];
    
    return cell;
}

5. Define the Custom Method for refresh control.

#pragma mark -  Custom Methods

- (void)refresh:(UIRefreshControl *)refreshControl {
    // Do your job, when done:
    [tableDataArray addObject:@"Priya"];
    [_tableView reloadData];
    [refreshControl endRefreshing];
}

Now run the code and pull down the tableview to see the desired result.

You can download the whole source code from here.
Please have Your valuable feedback.
Enjoy Coding :)

Tuesday, 2 August 2016

Search Bar in UITableview in iOS using Objective C

Today we will learn how to use UISearch bar to give search option in UITableview in ios.

Steps:
1. Create a new Xcode Project Lets Say(DefaultSearchBarIntableView).
2. Go to MainStoryBoard and add a tableview, Searchbar and a label.
3. Now We need to set proper connection and delegate for UISearchBar and UITableView.
4. Check on Shows Cancel Button.
5. Now go to your ViewController.m and write the following code.


#import "ViewController.h"

@interface ViewController ()<UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *searchResult;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;

@end
@implementation ViewController
{
    NSMutableArray *tableDataArray;
    BOOL searchEnabled;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    tableDataArray = [[NSMutableArray alloc]initWithObjects:@"Ram",@"Shyam",@"Mohan",@"nilesh", nil];
    self.searchResult = [NSMutableArray arrayWithCapacity:[tableDataArray count]];
    
    // 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 -  tableView Delagate Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (searchEnabled) {
        return [self.searchResult count];
    }
    else{
        return [tableDataArray count];
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    if (searchEnabled) {
        cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row];

    }
    else{
        cell.textLabel.text = [tableDataArray objectAtIndex:indexPath.row];
    }
       return cell;
}



#pragma mark - Search delegate methods

- (void)filterContentForSearchText:(NSString*)searchText
{
    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"SELF CONTAINS %@",
                                    searchText];
    
    _searchResult = [tableDataArray filteredArrayUsingPredicate:resultPredicate];
    [_tableView reloadData];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchBar.text.length == 0) {
        searchEnabled = NO;
        [self.tableView reloadData];
    }
    else {
        searchEnabled = YES;
        [self filterContentForSearchText:searchBar.text];
    }
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    searchEnabled = YES;
    [self filterContentForSearchText:searchBar.text];
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    [searchBar setText:@""];
    searchEnabled = NO;
    [_tableView reloadData];
    
}

@end

6. Now run the Code and see the desired outPut.


 7. You can download the whole source code from here.

Please do give Your valuable feedback.
Enjoy Coding.  :)

Ads Inside Post

Blogger Widget