---
title: "iOS : Creating sample using UILocalNotification class"  
description: "Local Notifications make use of a class called UILocalNotification in the UIKit framework. By configuring the properties of UILocalNotification, you c"  
author: "Tarun Kumar"  
published: 2015-07-24  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1833/ios-creating-sample-using-uilocalnotification-class  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 4 minutes  

---

# iOS : Creating sample using UILocalNotification class

Here we are going to create a sample of displaying [notification](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) on our click. In this sample we will use UILocalNotification and UISwitch classes that are provided in UIKit [framework](https://yourviews.mindstick.com/view/81101/time-for-improvement-in-healthcare-facility-framework), before creating this sample we will discuss about UILocalNotification and UISwitch:

By configuring the [properties](https://yourviews.mindstick.com/view/81845/now-it-s-possible-to-predict-properties-of-any-molecule) of UILocalNotification, you can use the UIApplication class to trigger the notification to arrive on screen either immediately or at a specified time and date. UIApplication handles putting the notification on the screen when required. Since the release of iOS 4, Apple introduced a new type of notification called Local Notification. This makes pushing a notification much simpler. No more server side programming. No more [Internet connection](https://answers.mindstick.com/qa/94706/my-google-chrome-lags-very-much-in-spite-of-a-good-internet-connection-how-do-i-troubleshoot-it) is required. You can schedule a notification with simple API and it’ll be fired up at a proper time.

To demonstrate the usage of UILocalNotification we’ll build a simple, on this sample we will provide a list of colors and a switch on each cell, when the user switch on any of the available color on the list, a notification reminds users about the selection of color. The UISwitch control is used to display the state of a Boolean object in iOS8. You use the UISwitch class to create and manage the On/Off buttons.

##### Creating a sample using UILocalNotification class

• create a new project using the “Single View [application](https://www.mindstick.com/articles/12824/calculator-application-in-android)” temple.

• create the [user interface](https://www.mindstick.com/blog/302016/what-is-user-interface-and-what-are-its-benefits) and add the [table view](https://www.mindstick.com/forum/34084/how-to-change-the-height-of-table-view-in-ios). Select “SimpleTableViewController.xib” to switch to the Interface Builder.

• In the Object Library, select the “Table View” object and drag it into the view.

• [Your screen](https://answers.mindstick.com/qa/103593/how-to-sharing-your-screen-on-skype) should look like below after inserting the “Table View”.

\

![iOS : Creating sample using UILocalNotification class](https://www.mindstick.com/mindstickarticle/e9321118-9667-41d0-9c20-6dc953a30150/images/3d236a05-cc15-4c1b-8694-1bf3d93fc2c9.png)

##### Define a local notification:

code of ViewController.m file

```
@implementation ViewController@synthesize switchObject,switchLabel,arrayList;// Called after the controller’s view is loaded into memory.- (void)viewDidLoad{    [super viewDidLoad];
    // defining list of color into the arrayList    arrayList = [NSMutableArray arrayWithObjects:                 @"Red",@"Green",@"Blue",@"Yellow",@"Orange",                 @"White",@"Pink",@"Black", nil];    }// this method return the no of rows in the arrayList-(NSInteger)tableView:(UITableView *)tableView                        numberOfRowsInSection:(NSInteger)section{  return  arrayList.count;}// this method insert the value in each cell of UITableView - (UITableViewCell *)tableView:(UITableView *)tableView                      cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *simpleTableIdentifier = @"TableCell";   
    UITableViewCell *cell = [tableView     dequeueReusableCellWithIdentifier:simpleTableIdentifier];       if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:               UITableViewCellStyleDefault reuseIdentifier:               simpleTableIdentifier];        switchObject = [[UISwitch alloc] init];        switchObject.tag = indexPath.row;        [switchObject addTarget:self action:@selector(switchChanged:                                withobject:) forControlEvents:                                UIControlEventValueChanged];        [switchObject setOn:YES];        cell.accessoryView = switchObject;    }        cell.textLabel.text = [arrayList objectAtIndex:indexPath.row];    return cell; }// this method is colled when switch is being ON or OFF on every action-(void)switchChanged:(UISwitch *)switcher withobject:                     (NSIndexPath*)index{     // here we defining and initializing UILocalNotification       UILocalNotification *localNotification =                          [[UILocalNotification alloc] init];    // here we defining the time when the notificaition will be displayed    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];  // here we defining the Body that is Displayed on swithching the switch    localNotification.alertBody =[arrayList objectAtIndex:switcher.tag];    // this method is used to call the localNotification    [[UIApplication sharedApplication] scheduleLocalNotification:                                       localNotification];}@end
```

This basically setup a local notification to fire in seconds in the future from when the app view comes on screen.

Code of ViewController.h file

```
#import <UIKit/UIKit.h>@interface ViewController : UIViewController           <UITableViewDelegate,UITableViewDataSource>@property(nonatomic,strong) IBOutlet UISwitch *switchObject;@property IBOutlet UILabel *switchLabel;@property NSMutableArray *arrayList;@end
```

**\
code of** *AppDelegate.m* **file**

**-** this delegate method is used to register the notification method. add it into AppDelegate.m file.

```
// here we registering our local notification- (void)application:(UIApplication *)application didReceiveLocalNotification:                    (UILocalNotification *)notification {    [self showAlarm:notification.alertBody];}
```

Display the alert\

**code in** *AppDelegate.m* **file**

```
// customized method- (void)showAlarm:(NSString *)text {    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:                             @"Notification"                             message:text                             delegate:nil                             cancelButtonTitle:nil                             otherButtonTitles:nil];    [alertView show];    [self performSelector:@selector(test:) withObject:                          alertView afterDelay:2];}
```

```
// this method is used to dismiss the notification after some time-(void)test:(UIAlertView *)x{    [x dismissWithClickedButtonIndex:-1 animated:YES];}
```

Now, run the application and you will see the switches are added in each cell of Table View. Okay, let’s compile and run the app. :

![iOS : Creating sample using UILocalNotification class](https://www.mindstick.com/mindstickarticle/e9321118-9667-41d0-9c20-6dc953a30150/images/a3900a40-886a-45e4-bbfa-7a9e7bfa27bd.png)\

now, when you switch the button a notification will be displayed for some time and after it will be disappear.

[ Notification is device depended it can appear every device differently ]\

![iOS : Creating sample using UILocalNotification class](https://www.mindstick.com/mindstickarticle/e9321118-9667-41d0-9c20-6dc953a30150/images/6e22e6f0-58c3-4101-beee-84455e094199.png)\

---

Original Source: https://www.mindstick.com/articles/1833/ios-creating-sample-using-uilocalnotification-class

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
