Start Using Helper Class Now! — Part 2

Globally reusable functions with example

Mohaiminul Islam
InfancyIT
3 min readMar 12, 2018

--

This article is a follow up of a previous article. If you haven’t read it yet, read it first here.

In my previous article we discussed on using global constants to reuse color literals and other constants that’s needed globally. Today, it’s all about extending the functionality of our Helper class with globally reusable functions.

A simple functionality that we use almost everywhere in our app is- showing alerts and status banners. A sample alert view presented from the ViewController class can be like this,

As we are too smart to copy it all over those places we need it, we write out a function in our previously made Helper.swift file, mirroring the above implementation of UIAlertController class.

Now we have a globally available alert function. Whenever we need an alert dialog of this sort, we can just write from our view controller,

Helper.app.showAlert(title: "Alert", message: "Deleting mail!", vc: self)

….and we are done.

.

.

PRO DEV TIP:

For status banners in iOS there are some pretty good third-party solutions out there. If you use cocoapods, you can use several pods for this purpose. If you’re new to using cocoapods in iOS, refer to this.

.

.

I personally use NotificationBannerSwift.

We can leverage the power of this package along with our Helper class to come up with a neat way to show notification/status banner messages with alert styles.

First we need to install NotificationBannerSwift via the instructions inside the readme. Then we need to import it inside our Helper.swift file above Helper class.

import NotificationBannerSwift.
.
class Helper { static var app: Helper = { return Helper() }().
.
.
}

Now just add the function showStatus() after the alert function we wrote before in our Helper class.

Now that we declared our function globally we can just call this function with appropriate params from any of our viewcontroller classes.

Helper.app.showStatus(status: "Mail Sent!", style: .success, vc: self)

. . .

Productivity rages on as devs come up with reusable ideas!

Is there any internet around here somewhere?

How do i know if there is internet before I execute a network exhaustive task?

If you have ever asked yourself this question while building your iOS app, this is for you.

Apple has a handy SystemConfiguration class which can check for Network Reachability, in more humane terms, it can check for you if there is internet so you can check for connection and make requests and network calls while online.

First, import SystemConfiguration class in our helper file.

import SystemConfiguration

Add the following isInternetAvailable() function after the functions we have declared so far in our Helper class.

The function itself just pings through socket to know if there is internet and based on returned flags the function gives us a boolean value. Now when we need to know if internet is available we can do this,

if(Helper.app.isInternetAvailable()){    //network exhaustive calls
} else {
Helper.app.showStatus(status: "NO INTERNET", style: .danger, vc: self)
}

And with minimal effort we have made a global network reachability utility function without any third party resource!

That’s it for today folks! Check out my other articles in here.

--

--

Mohaiminul Islam
InfancyIT

Software Engineer (iOS, Web) & a reclusive learner