Converting Timestamp to NSString Providing the Difference to “Just Now”

If you got a timestamp, e.g. from an SQL database entry, and you need a NSString on iOS that tells you how many seconds, minutes, hours, etc. ago this event happened (to display to the user), NSDateComponents is your friend.

Here is a small utility method that illustrates the conversion:

+ (NSString*)timeAgoString:(NSString *)timestampString
{
    double eventSecondsSince1970 =
        [timestampString doubleValue] / 1000.0; // milliseconds to seconds
    NSDate *eventDate =
        [NSDate dateWithTimeIntervalSince1970:eventSecondsSince1970];
    NSCalendar *gregorian =
        [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]
        autorelease];
    int unitFlags = NSYearCalendarUnit |
                    NSMonthCalendarUnit |
                    NSDayCalendarUnit |
                    NSHourCalendarUnit |
                    NSMinuteCalendarUnit |
                    NSSecondCalendarUnit;
    NSDateComponents *comps =
        [gregorian components:unitFlags
        fromDate:eventDate toDate:[NSDate date] options:0];

    NSString *timeAgoString = [NSString string];
    if ([comps year] > 0) {
        timeAgoString =
            [NSString stringWithFormat:@"%i years ago", [comps year]];
    }
    else if ([comps month] > 0) {
        timeAgoString =
            [NSString stringWithFormat:@"%i month ago", [comps month]];
    }
    else if ([comps day] > 0) {
        timeAgoString =
            [NSString stringWithFormat:@"%i days ago", [comps day]];
    }
    else if ([comps hour] > 0) {
        timeAgoString =
            [NSString stringWithFormat:@"%i hours ago", [comps hour]];
    }
    else if ([comps minute] > 0) {
        timeAgoString =
            [NSString stringWithFormat:@"%i mins ago", [comps minute]];
    }
    else if ([comps second] > 0) {
        timeAgoString =
            [NSString stringWithFormat:@"%i secs ago", [comps second]];
    }
    return timeAgoString;
}

Communication Between View Controllers in iOS

Basically, there are three different ways to implement a communication between view controllers in iOS:

  • If the view controllers are closely related to each other, you can give them instance variables that point to each other. This is the easiest way to send messages between view controllers, but it’s the least adaptable. You should avoid this.
  • You can create a delegate protocol that specifies the messages that one view controller can send to the others. Other view controllers can then adopt to this protocol. Your view controller does not need to know to which type of view controllers it sends the message, only that they conform to the protocol.
  • If there is no relation between the view controllers, the notification system can be used. It also makes sense if multiple view controllers are interested in the messages of one view controller. Posts can go to NSNotificationCenter, where interested view controllers have registered for that notification.
I always talked about view controllers here, but the same is true for other objects, e.g. views.

Overlay with Gradient and Transparency

If you want to add a gradient to a UIView with transparency, so that you can e.g. put it on top of a UIImageView as container for some text, and you can rely on iOS 3.0+ being available, you might use a layer for this.

You can safely rely on iOS 4.0+ being installed on people’s iPhone and iPad nowadays. Already back in January, 84% of iPhones were running iOS 4 according to Chitika, and even iOS 5 was already installed on 33% of all iOS devices after less than one week of availability according to Localystics.

Include QuartzCore/QuartzCore.h to your source file, and you can create a gradient overlay from solid black to full transparency this way:

UIView *overlayContainer =
  [[[UIView alloc] initWithFrame:
    CGRectMake(0, 0, kSizeWidth, kSizeHeight)]
    autorelease];
overlayContainer.backgroundColor = [UIColor clearColor];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = overlayContainer.bounds;
gradient.colors = [NSArray arrayWithObjects:
  (id)[[UIColor blackColor] CGColor],
  (id)[[UIColor clearColor] CGColor], nil];
[overlayContainer.layer insertSublayer:gradient atIndex:0];
[self.view addSubview:overlayContainer];

iOS5 Install Base

Interesting post about iOS5 install base as of October 17th, 2011:

The latest and greatest Apple mobile operating system, iOS 5, has been available to the public for less than a week, and yet it’s already been downloaded enough to power 33% of all eligible iOS devices. If you remove the newly-launched iPhone 4S (which ships with iOS 5), that number drops a tiny bit, but still remains an impressive 31%.

Looking at devices, we see that the iPad 2 and iPhone 4are leading the charge, with 36% and 35% iOS 5, respectively. Original iPads clock in with 33% iOS 5 proliferation, followed by iPhone 3GS (27%), iPod Touch 3rd Generation (23%), and iPod Touch 4th Generation (17%).

Posted in iOS