Click here to Skip to main content
15,868,142 members
Articles / Mobile Apps / iOS / iOS7
Tip/Trick

Adding a Notification Message to an iOS App

Rate me:
Please Sign up or sign in to vote.
4.27/5 (5 votes)
13 Jun 2014CPOL4 min read 10.7K   90   5  
How to add an fade-in message to your iPhone app in 5 minutes

Introduction

In this tip, I'm going to describe a useful little add-in that you can add to your XCode iPhone/iPad projects, which lets you easily display a popup message at the bottom of the screen.

Image 1

It'll take you about 5 minutes to modify your XCode project to use this control, and once you've done it, you can display a notification message in any of your UIViewController screens using one line of code:

C++
[self showStatusMessage:@"Loading Customer records.."];

That one line of code will make your message fade onto the screen, wait for 2 seconds, then fade it away again.

I know, it may sound pretty dumb, but in use, this is a really cool way just to let your users know that something has happened, without it "getting in the way" of your user-interface and without making your Objective-C code any more unreadable.

Screenshots don't really do this control justice. I often have functionality in my app where, say, a web service gets called, and it might take a few seconds for the response to come back.

With this control, I can easily let the user know when a service has successfully finished running, without having to specifically add a "status" UILabel to my screens, or display a modal "Record was successfully saved" HUD dialog.

With this control, I just need that one line of code, to unobtrusively show the user that their database record has been saved, their web service has been called, and so on.

This is my favourite iPhone control!

Adding the 'StatusPopup' UIView

Attached are the .m and .h files for the new StatusPopup control, which is the new UIView which gets faded onto the screen.

It contains just one function, which takes your string, measures it (to work out how large the UIView needs to be), and adds a drop-shadow and a border to the UIView.

I've chosen to position the UIView horizontally-aligned and 20 pixels from the bottom of the screen, but you can, of course, change this if you want.

It can also cope with very long strings, wrapping them onto several lines, but just remember, your users will only get 2 seconds to read the message!

Image 2

When I wrote this class, my first instincts were to just add this UIView to one particular screen. I would add a new variable (@property) to the screen's .h file, then, when I wanted to display the popup, I would initialise it, and call its "doTheAnimation" member function:

So, my .h file would contain something like this:

C++
#import "StatusPopup.h"

@interface LoginViewController

@property (strong, nonatomic) StatusPopup* statusPopup;

@end

And my .m file would contain this...

C++
@synthesize statusPopup;


-(void)saveToDatabase
{
    //
    //  (Code to save some data to the database)
    //

    . . .

    self.statusPopup = [[StatusPopup alloc] init];
    [self.statusPopup doTheAnimation:self.view labelText:@"Successfully saved."];
}

Yup, absolutely nothing wrong with that.

Except, I then found myself repeating this code in my next UIViewController. And the next. And the next.

And then found I needed extra code - what happens if I wanted to show a new status message, but there was already one on the screen? Suddenly, I needed to add fixes in each version of this code.

Categories

As this is a control which would be useful in most of my iPhone screens, I decided to write a Class Extension to the UIViewController class, so that all of my screens could easily use this control.

And this is where Objective-C categories come in.

What we can do is to add this StatusPopup control to all of our UIViewController screens in one go, then add a helper function, to look after displaying the message for us.

The UIViewHelper.h and UIViewHelper.m files do this for us.

The .h file merely adds a new showStatusMessage function to the existing UIViewController class, along with a StatusPopup variable, as described above. So, the .h file looks like this:

C++
#import <UIKit/UIKit.h>
#import "StatusPopup.h"

@interface UIViewController (StatusMessage)
@property (strong, nonatomic) StatusPopup* statusPopup;
-(void)showStatusMessage:(NSString*)statusMessage;
@end

Now, in categories, you cannot use the synthesize command. So we can't use this line in our .m file:

C++
@synthesize statusPopup;    // This will throw an error!

If you were to try this, XCode will throw up this error:

@synthesize not allowed in a category's implementation.

Instead, we need to go back to the runtime.h to allow us to load and save the value of our new property:

C++
#import "UIViewHelper.h"
#import <objc/runtime.h>

@implementation UIViewController (StatusMessage)
@dynamic statusPopup;

NSString const *key = @"kKeyStatusPopup";

- (void)setStatusPopup:(StatusPopup*)test
{
    objc_setAssociatedObject(self, &key, (id)test, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(StatusPopup*)statusPopup
{
    return objc_getAssociatedObject(self, &key);
}

-(void)showStatusMessage:(NSString*)statusMessage
{
    if (self.statusPopup != nil)
    {
        [self.statusPopup removeFromSuperview];
        self.statusPopup= nil;
    }
    self.statusPopup = [[StatusPopup alloc] init];
    [self.statusPopup doTheAnimation:self.view labelText:statusMessage];
}

@end

(My thanks to this article for showing me how to do this !)

As you can see, other than getting and setting the StatusPopup value, there's really not much to this UIViewController class extension, just our new showStatusMessage function.

Adding the Code to your Project

I said earlier that adding this popup to your code would take you about 5 minutes.

All you have to do is copy the four files into your project - I've chosen to add mine into a Helpers folder...

Image 3

...then select your project's .pch file, and add one line of code:

C++
#import "UIViewHelper.h"

And that's it.

After building your project, you can go into any of your UIViewController screens, and use the new showStatusMessage function. XCode will even show you its parameters:

Image 4

It doesn't get much easier than that!

Final Thoughts

Please leave some feedback if you find any issues using this code, and don't forget to leave a rating if you find this code useful.

You can also find some other useful walkthroughs on my blog:

Thanks for reading.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Switzerland Switzerland
I'm a C# developer, working in finance in Zurich, Switzerland.

Author of the PartnerReSearch iPad app, which was a winner of a "Business Insurance Innovation Award" in 2013, and a TechAward2014 "Innovation of the year" award in 2014.

Objective-C is the 2nd hardest language I've ever learned, after German... Wink | ;-)

Comments and Discussions

 
-- There are no messages in this forum --