To check if a Credit/Debit Card is about to expire or has expired iOS is very simple. I came across applying this logic in an app but took me sometime to figure out as I’m a mid-level iOS developer.
Problem is to check whether a credit card is about to expire or has expired. So how to do it?

Solution is very simple we will use NSDateFormatter and play along with it.

First of all what you need to know is how a credit card expiration date looks like in my case it’s 06/16 which is 06 month of 2016. So Let’s start coding.

Make a method

A simple method which returns true if it is about to expire and false of not. The expiry date is the given date in string and in my example I’ll use 06/16 in the same format as written.

So second step is to break the string from ‘/’ as we need to check month and year separately.

We take an array and give it the string whose components are separated by ‘/’ and then stored it’s two components separately in NSString.
Next step is to take current date and do the same with it.
As you can see we have set DateFormat as MM/YY same as we are getting in our arguments.
We fetched the current date by using NSDate gave it our dateformatter and than did the same code as above to separate and store string.
If you NSLog(@”%@ %@ and %@ %@”,monthFromExpiry, yearFromExpiry, monthFromDate, yearFromDate); you’ll see you have the exact same data on both sides. But there is just one glitch because of which I’m doing this next step there might be a chance that user enters the value let’s assume  9 instead of 09 and in NSString they are not equal. So to encounter this glitch I’m converting them to int and then apply check to them as:
Initialized and stored intValue of the strings.
And now the fun part the if logic.
Now I’ll break the if part for better understanding. There are 3 conditions in this if condition.
(ye==yd&&me==md) If year if expiry year and year of current date is equal and month of expiry and month of current date is equal.
(ye==yd&&me<md) If year if expiry year and year of current date is equal and month of expiry is less than the month of current date.
(ye==yd&&me==md+1) If year of expiry is equal to the current date month and month of expiry is equal to current date month +1. You can change this 1 to 2 or 12 or 24 this is the time before you want to notify the user that his/her card is expiring. I’ve to notify user 1 month before the expiration date so I’m using 1.
That’s it, if any one of this is true return YES otherwise NO. Here is the full code of this method.
Now after calling this method form any method like [self isItaboutToExpire:@”01/16″]; you’ll get positive results. 🙂


Tags : credit card is about to expire or has expired ios
Chaudhary Talha

The author Chaudhary Talha

I'm an iOS developer by profession and UI/UX designer by passion. All my tutorials are based on my personal experiences and whenever I learn something new I share it with the world.

1 Comment

  1. // Getting Current Date and Breaking it
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"MM/YY"];
    NSDate *now = [NSDate date];
    NSString *currentDate = [dateFormatter stringFromDate:now];
    NSArray *currentMonthWithYear = [currentDate componentsSeparatedByString:@"/"];

    NSUInteger currentMonth = [currentMonthWithYear[0] integerValue];
    NSUInteger currentYear = [currentMonthWithYear[1] integerValue];
    NSUInteger expiryMonth = [_monthTextField.text integerValue];
    NSUInteger expiryYear = [_yearTextField.text integerValue];

Comments are closed.