Psellos
Contemporary Development With Functional Programming

Tiny iOS App in One Source File

May 22, 2012

Psi is a tiny, but complete, iOS test app in one source file, around 40 lines of Objective C. It’s nothing fancy, it just draws a Greek psi character on the screen. But I thought it might be useful to other folks. Sometimes it’s just nice to have a little example.

Psi example app

Psi can be very simple because it’s targeted at iOS 5, when Apple generalized the startup process to add storyboard support. A side effect is that it’s now possible to have no startup file (nib or storyboard file) at all. Psi shows how this can work. You can find a more detailed description of the iOS app launch sequence in a blog post by Ole Begemann, which is where I got the idea.

You can download the binary of Psi and run it directly in the iOS Simulator:

To try out this version, unzip and double click on the launcher app PsiLauncher.app. You need to have Xcode and the iOS 5.0 Simulator installed for this to work. Psi appears on the second screen of apps—swipe to the left to see it. If it’s not there, you might need to change the version number to 5.1 in the Hardware -> Version menu.

Psi is so exceptionally simple that it’s really not very interesting to run—the figure above shows everything there is to see. However, it might be interesting to see how I packaged up an OS X app to run Psi in the simulator.

You can also download the sources and an Xcode project for building and running in the iOS Simulator or on an iOS device:

To try out this version, unzip and double click on the Xcode project, Psi.xcodeproj.

You can also build and run from the command line using the runsim script if you are so inclined. After unzipping the sources, here’s what you need to do:

$ cd psi-1.0.0
$ make -f Makefile.iossim Psi
$ runsim Psi

If Psi doesn’t show up, set the simulated iOS version number of the iOS Simulator to 5.1 with the Hardware -> Version menu. runsim places apps into the iOS 5.1 simulator.

For more about the runsim script, see Run iOS Simulator from the Command Line (Improved).

Finally, here is the full source of Psi.

/* psi.m     Tiny iOS App in one file
 *
 * (It just draws a Greek letter Psi.)
 *
 * Copyright (c) 2012 Psellos   http://psellos.com/
 * Licensed under the MIT License:
 *     http://www.opensource.org/licenses/mit-license.php
 */
#import <UIKit/UIKit.h>

@interface AppDelegate : NSObject <UIApplicationDelegate> {
}
@end

@implementation AppDelegate : NSObject
- (BOOL) application: (UIApplication *) anAppl
                didFinishLaunchingWithOptions: (NSDictionary *) opts
{
    UIViewController *vc = [[UIViewController alloc] init];
    UIWindow *w =
        [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
    UILabel *l = [[UILabel alloc] initWithFrame: [w bounds]];
    l.font = [UIFont fontWithName: @"MarkerFelt-Thin" size: 300.0];
    l.text = [NSString stringWithUTF8String: "\xce\xa8"];
    l.textColor =
        [UIColor colorWithRed: 0.141 green: 0.251 blue: 0.439 alpha: 1.0];
    l.textAlignment = UITextAlignmentCenter;
    l.backgroundColor =
        [UIColor colorWithRed: 0.800 green: 0.333 blue: 0.000 alpha: 1.0];
    vc.view = l;
    w.rootViewController = vc;
    [w makeKeyAndVisible];
    return YES;
}
@end

int main(int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
    [pool release];
    return retVal;
}

If you have any comments, corrections, or suggestions, leave them below or email me at jeffsco@psellos.com.

Posted by: Jeffrey

Comments

blog comments powered by Disqus