Extract (uncrush) Images from an iOS App

Often I’ll see an app incorporate a new UI implementation that really impresses me. Still being a learner, I always wonder how these elements are coded or put together — is it an image trick? Maybe a CGGradientLayer? How are they able to achieve so much speed? It’s times like these that it helps to get a small peek under the hood. Like Mac apps, iOS apps are bundled together basically as a zip file — you can actually change the IPA extension to ZIP and unarchive the app very easily. The limitation here is Xcode compresses (or ‘crushes’) the png’s used in an app to keep the file size down. Luckily, since iOS 3.2, the command line tool that compressed these images included the added functionality of decompressing them. Peter Boctor (of iDevRecipes) wrote a Ruby script called App Crush that would uncrush the png files (with instructions here) but it hasn’t been updated in about a year. Since then, Apple has changed how Xcode is deployed (through the App Store these days) and the location in which they put it. It’s very easy to update the Ruby script to find the new Xcode and several have. I wanted to make it just a tad easier and created an Applescript Droplet that allows you to drag the IPA onto it and uncrushes the png files. You can download it here. I’ve also made this version’s source available here. This is my first attempt at an AppleScript Droplet (which seems like a dying tech) so if you see something that could be done better, let me know!

Usage instructions:

  1. If you don’t know the location of the IPA (app) you’d like to uncrush, open iTunes and click the apps tab, right click on the app you’d like and click “Reveal in Finder”
  2. Drag IPA onto the App Uncrusher
  3. It’ll take up to a few minutes to finish and you’ll have a folder called “APPNAME images” on your desktop that should be full of pngs.

Be ethical with this. Don’t steal another app’s images and call them your own.

Using custom fonts on iOS (iPhone iPad)

This is a quick walk-through on using a font other than those supplied by Apple on an iOS device.

For reference purposes, you can find out what fonts are available to you “out of the box” by checking out this comprehensive list:
http://iosfonts.com/

I’m going to be using Bebas for my example, a great font created by Dharma Type. You can pick it up here: http://www.dafont.com/bebas.font or use a font of your own choice. It’s important to note you should check a font’s license before you use it in an app you intend to distribute in the app store.

If your font’s not installed on your Mac, go ahead and install it. Before we get too deep into coding and while you’re in or around Font Book let’s go ahead and get the PostScript name of your font. You can do this by selecting your font from the list inside of Font Book and pressing Command + I to toggle the font information. The right side of the window will look like this:

The PostScript name is listed on the top, with Bebas, the PostScript name is simple… it’s Bebas but most are more complicated. Take the PTSans family for example: PTSans-Regular to PTSans-CaptionBold. Keep this PostScript name handy as we’ll reference it later.

Moving on let’s get the ttf file into an Xcode project.

I started with a Single View Application template, go ahead and get that going as normal. Inside my Supporting Files folder I’m going to create a group named “Fonts”. I’m going to drag BEBAS___.TTF into that directory and make sure “Copy items into destination group’s folder (if needed)” is checked. Click finish.

Next, open your app’s plist. Right click and add a row, we’re going to add the key “Fonts provided by application” which is an array of the ttf font files. Toggle that down and for Item 0 add BEBAS___.TTF.

Now you need to head over to your project’s build phases tab. Click to the “Copy Bundle Resources” and click the + icon to add a new item and choose BEBAS___.TTF.

Now, when your window looks like this, you’re ready to use the font in the application:

I put some simple code to create a UILabel in my viewDidLoad method like this:

    UILabel *bebasFlavoredLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 320, 44)];
    bebasFlavoredLabel.text = @"Bebas on iPhone";
    [bebasFlavoredLabel setFont: [UIFont fontWithName:@"Bebas" size:15]];

    [self.view addSubview: bebasFlavoredLabel];

On line 3 you see where we use [UIFont fontWithName:@"Bebas" size:15]. The name you use there is the PostScript name you found at the beginning. Go ahead and run:

Viola! Your font is ready to be used as you wish!

Lock screen “Now Playing” with MPNowPlayingInfoCenter

Note: Example project is available here

One of the great additions iOS 5 brought us is the ability to display meta data about currently playing tracks on the lock screen. This is a great convenience to users and a must if your app has background audio playing. I know I use this to great extent, especially when driving. Best of all, it’s actually quite simple to get going.

For the sake of this tutorial, we’re going to be focusing mainly on the MPNowPlayingInfoCenter and not much on how to play streaming audio. If you have questions, as always, please feel free to leave a comment. I, as I’ve stated in the past, am still fairly new to the iOS/Objective-C world so if you see something that makes you say ‘UR DOING IT WRONG!’, please let me know. If you’d like to review the details of MPNowPlayingInfoCenter, you can read the Apple documentation.

To get started, create a new project. I created one using the single view template but feel free to do whatever you’d like. Once you’ve created the project there are a few things we need to do to get the project setup. By default, simply playing audio won’t persist if the application leaves the foreground. We need to tell iOS we’d like to play background audio. To get started doing this, open up your application’s info.plist file and add a new row: “Required background modes” (UIBackgroundModes). This creates an array with Item 0 change the value to “App plays audio” (audio). I’ve got a screen shot of what this should look like:

Next, there are a few frameworks we need to link to our project:

  1. AVFoundation.framework
  2. MediaPlayer.framework
You can do this by click on your project, and selecting the Target and swiveling down “Link Binary with Libraries”, click the + at the bottom and begin typing those names. Once you’re done, it should look like this:
Now we’re ready to start playing audio. In my view controller, I’ve created a simple IBOutlet UIButton called playButton and linked it in the nib. I’ve also attached an IBAction, playButtonPress to the button’s touch up inside event.

You can see those items in myViewController.h:


@interface ViewController : UIViewController {

IBOutlet UIButton *playButton;

}

@property (nonatomic, retain) IBOutlet UIButton *playButton;

-(IBAction)playButtonPress:(id)sender;

Next I’m going to import the MPMoviePlayerController header to add a player to my view controller class:

#import <MediaPlayer/MPMoviePlayerController.h>

To add the player controller we’ll add these lines to the ViewController.h:

MPMoviePlayerController *audioPlayer;

and

@property (nonatomic, retain) MPMoviePlayerController *audioPlayer;

Swing over to your ViewController.m file and we’re going to add a few more headers:

#import <MediaPlayer/MPNowPlayingInfoCenter.h>
#import <MediaPlayer/MPMediaItem.h>
#import <AVFoundation/AVFoundation.h>

And we’ll synthesize our playButton and audioPlayer:

@synthesize playButton, audioPlayer;

In viewDidLoad we’ll initialize our audio session and audioPlayer and pre-load it with content from the web. We’re going to use Oliver Drobnik’s Cocoanetics podcast as our audio feed for this tutorial. Oliver has an awesome podcast packed with great info for iOS developers, you can find out more about it at http://cocoanetics.com.


[[AVAudioSession sharedInstance] setDelegate: self];

NSError *myErr;

// Initialize the AVAudioSession here.
if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&myErr]) {
    // Handle the error here.
    NSLog(@"Audio Session error %@, %@", myErr, [myErr userInfo]);
}
else{
    // Since there were no errors initializing the session, we'll allow begin receiving remote control events
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}

    //initialize our audio player
    audioPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://www.cocoanetics.com/files/Cocoanetics_031.mp3"]];
    
    [audioPlayer setShouldAutoplay:NO];
    [audioPlayer setControlStyle: MPMovieControlStyleEmbedded];
    audioPlayer.view.hidden = YES;
    
    [audioPlayer prepareToPlay];

It’s important we have line 12 as this is what tells iOS we want to receive any remote control events from the lock screen (i.e. backward, play, pause, forward). I’ve found that without that line your information will never display (here’s a link to my Stack Overflow question chronicling my journey to that discovery).

Moving onward, next we need to create our action that actually plays the audio and posts the information to the lock screen:


- (IBAction)playButtonPress:(id)sender {
    
    [audioPlayer play];
    
    Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
    
    if (playingInfoCenter) {

        
        NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
        
       
        MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: [UIImage imagedNamed:@"AlbumArt"]];
        
        [songInfo setObject:@"Audio Title" forKey:MPMediaItemPropertyTitle];
        [songInfo setObject:@"Audio Author" forKey:MPMediaItemPropertyArtist];
        [songInfo setObject:@"Audio Album" forKey:MPMediaItemPropertyAlbumTitle];
        [songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
        [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];


    }
}

Let’s go through this line by line starting with line 5. Lines 5 & 7 of our code make sure the class MPNowPlayingInfoCenter exists since this functionality was just added in iOS 5. Next, we create an NSMutableDictionary called songInfo that will contain the information for the lock screen. Next we create an MPMediaItemArtwork item that will store the image. We set our information on lines 13-16. These are the properties that display on the lock screen. Something to keep in mind is there are several 3rd party peripherals that interact with the iPhone/iPod that play music that might pull this information. There are a few additional properties you can assign that may be accessed by other devices and you can read about those in the Apple docs. Last and certainly not least, the magic happens with line 17 as we add the information to the lock screen! Viola!

Path 2.0 style animated splash screen (default.png)

I don’t use splash screens often but when I do, I want them to open like a book.

In all truth, I’m not a big fan of splash screens and even Apple recommends using a default.png that shows the controls (with no text) of the application:

Display a launch image that closely resembles the first screen of the application. This practice decreases the perceived launch time of your application.

Avoid displaying an About window or a splash screen. In general, try to avoid providing any type of startup experience that prevents people from using your application immediately.

from HIG Guidelines

However, some people love them and one app in particular has a nice implementation splash screen — Path 2.0. When you open Path, you’re greeted with their logo on a red version of the Apple linen texture that animates open like a book (or journal as that’s what Path considers themselves to be).

You can get the source for this project here: https://github.com/jaysonlane/OpenBook

Before we begin, let me preface this with a disclaimer: I am very new to animations in Cocoa so bear with me. If you spot unnecessary or inefficient code, please leave a comment and I’ll tidy it up.

If you haven’t seen the animation, hop on the app store and pick up a copy to see what we’re trying to accomplish. I’ve created a default png that we can use cleverly titled Math (like a Math book that opens, right?) You can download that here (retina) and here.

To get started, let me explain “the trickery” behind what we’ll be doing: we’re going to use the normal default splash system in place to display our default.png. In the App Delegate, once the application has finished launching, we’re going to create a UIImageView on top of our view of that same default.png. We’ll then animate that UIImageView, to rotate open to reveal our view.

So let’s go:

Create a new project, I created one using the single view template but this will work with whatever. Go ahead and set your default.png and default@2x.png to the images supplied. You can do this by clicking the project in the navigation pane on the left, click the Target and scroll down to launch images:

Open your AppDelegate.m and add the following code to your application didFinishLaunching or application didFinishLaunchingWithOptions function:


    //1. add the image to the front of the view...
    UIImageView *splashImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [splashImage setImage: [UIImage imageNamed:@"Default"]];
    [self.window addSubview:splashImage];
    [self.window bringSubviewToFront:splashImage];

    //2. set an anchor point on the image view so it opens from the left
    splashImage.layer.anchorPoint = CGPointMake(0, 0.5);

    //reset the image view frame
    splashImage.frame = CGRectMake(0, 0, 320, 480);

    //3. animate the open
    [UIView animateWithDuration:1.0
                          delay:0.6
                        options:(UIViewAnimationCurveEaseOut)
                     animations:^{

                         splashImage.layer.transform = CATransform3DRotate(CATransform3DIdentity, -M_PI_2, 0, 1, 0);
                     } completion:^(BOOL finished){

                         //remove that imageview from the view
                         [splashImage removeFromSuperview];
                     }];

Three things are happening here…

1) We create a new UIImageView and add it to the top of the view
2) We set an anchor point on the left side of the image to make it open from the left and then reset the frame to the full size of the view
3) We animate the UIImageView and remove it from the view on completion

That’s it, it’s that simple.

NSDateFormatter

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"YYYY-MM-dd"];

I’ve enjoyed PHP’s date() documentation but have yet to find comprehensive documentation for NSDateFormatter. I have not verified all of the following so if you find an error, leave a comment and I’ll try to get it fixed as quickly as possible. A ton of this information was gathered from Alex Curylo.

Character Description Example Returned Value
a Ante Meridiem and Post Meridiem AM/PM
A Millisecond of the Day 0..86399999
c/cc Numeric representation of day of the week 1..7
ccc Abbreviated day of the week Sun, Mon, Tue…
cccc Written day of the week Sunday, Monday, Tuesday…
d 0 padded Day of Month 1..31
D 0 padded Day of Year 01..366
e Day of Week with leading zero 01..07
E..EEE Sun, Mon, Tue…
EEEE Sunday, Monday, Tuesday…
F Week of Month, first day of week = Monday, with leading zero 1..5
g Julian Day Number (number of days since 4713 BC January 1)
G..GGG Era Designator Abbreviated BC, AD
GGGG Era Designator Before Christ, Anno Domini
h Hour (12 hr) with leading zero 1..12
H Hour (24 hr, starting at 0) with leading zero 0..23
k Hour (24 hr, starting at 1) with leading zero 1..24
K Hour (12 hr) with leading zero 0..11
m Minute with leading zero 0..59
s Second with leading zero 0..59
S Rounded sub-second
v..vvv General GMT Timezone Abbreviation GMT
vvvv General GMT Timezone Name Atlantic/Azores
z..zzz Specific GMT Timezone Abbreviation
zzzz Specific GMT Timezone Name
Z RFC 822 Timezone +0000
L..LL Month with leading 0 01..12
LLL Month abbreviation Jan, Feb, Mar…
LLLL Full Month January, February, March…
w Week of Year, 1st day of week is Sunday, 1st week of year starts from the last Sunday of last year, with leading zero 01..53
W Week of Month, 1st day of week = Sunday, with leading 0 01..05
M..MM Month of the year 1..12
MMM Month Abbreviated Jan, Feb, Mar…
MMMM Full Month January, February, March…
q..qq Quarter of the year 1..4
qqq Quarter abbreviated Q1, Q2, Q3, Q4
qqqq Quarter written out 1st quarter, 2nd quarter, 3rd quarter…
Q..QQ Quarter of the year 1..4
QQQ Quarter abbreviated Q1, Q2, Q3, Q4
QQQQ Quarter written out 1st quarter, 2nd quarter, 3rd quarter…
y/yyyy Full Year 2012, 2013, 2014…
yy..yyy 2 Digits Year 12, 13, 14…
Y/YYYY Full Year, starting from the Sunday of the 1st week of year 2012, 2013, 2014…
YY/YYY 2 Digits Year, starting from the Sunday of the 1st week of year
u Year

Icon already includes gloss effects but still glossy!

When we released Namely for iPhone, the first few days the app was in the store, the icon was glossy. This was not our intention. When you view the app in the store, it had the gloss effects automatically applied by Apple but once you installed it, the icon displayed on the springboard without the gloss effect (as intended). This was pretty annoying.

We had UIPrerenderedIcon (or ‘Icon already includes gloss effect’) in our info.plist but it still was having this issue with the app store. I did a bit of research and found that some people used jpg instead of png when uploading their 512×512 icon to the app store. You can only edit your icon when you’re app is in some sort of approval or waiting for approval status, not when it’s actively available in the store. I had a few changes I wanted to push in an update so I came up with 1.0.1 and uploaded it to the store. I changed to the jpg and still had gloss!

I got to looking around in the plist and realized that the UIPrerenderedIcon was nested inside of “Icon files (iOS 5) >> Primary Icon >> Icon already includes gloss effect”. So I put simply moved it out into the root of the plist and rebuilt. Submitted to the app store and viola! no more gloss.

My guess is because this key was nested the store didn’t see it so it put the gloss effects on as if the key were not there.