Project: Swipe2Pop

Project: Swipe2Pop

Problem

I was reading Ars Technica on my iPad while laying down in bed. I was holding my iPad in portrait mode with two hands. But each time I had to navigate back to the article list I had to release my right hand to press the home/back button at the top left of the screen. This made me think in motions.

Solution

Apple is using swipe gestures everywhere. Four finger swipe left/right on the iPad switches between apps, the same on Mac OS X Lion but between spaces. Even the browser got two finger swipe left/right to go back/forward in the browser. This made me think: why not on the iPad?!
So I made the Swipe2Pop category for UINavigationController.

Just use two fingers and swipe to the right on the iPad and you will pop the current controller on the navigation controller!

This project is now on Github. Just fork it!

Binary Search and Localized Sort in Objective-C

I had an issue where I had an list of words with about 57000 word, and they were all in swedish… Which makes it really hard på Objective-C to sort (with our å, ä, ö characters). And because the list contained so many words and the comparison should be really fast I decided to go with a binary search algorithm. I did some benchmarking and after the list was sorted, which is needed for binary searcg to work, the search took about 0.002 seconds to find the specific word in a 57000 items array, on a iPhone 4. Which is pretty fast.

First I did try with the [NSArray indexOfObject:] to see if the object exists. It was really fast on the simulator, but when I tried it on the device it took about 3 seconds to find the word on the main thread, which was really unacceptable.

In the swedish alphabet the characters å, ä, ö is the three last ones. But are treated as a, a, and o respectively.
And because Objective-C thinks everything is UTF-8 they reorder the alphabet some.

The sorting and searching code can be found at github

Usage
I’ve got a unsorted .txt-file with swedish names.

Göran
Örjan
Sven
Nils
Simon
Peter
Johan

Implmentation

- (void)search {
    // Loading file
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"names" ofType:@"txt"];
    NSString *file = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

    // Split the .txt-file with newline character
    NSArray *names = [file componentsSeparatedByString:@"\n"];

    // Sorting the array
    names = [names sortedArrayUsingComparator:[Utils compareBlock]];

    // Does 'Örjan' exists?
    int index = [Utils binarySearch:@"Örjan" array:names];
    // index = 6;

}

How to: AirPlay from your Mac to your Apple TV without iTunes

I love when you’re able to solve problems in your real life with programming. I had to be able to play YouTube videos on my Apple TV without saving them to the hard drive, converting them and throw ‘em in to iTunes. And to get the Apple Remote and navigate to the video on the Apple TV was not on the map.  After a while of googling I found that is was possible to send videos to the Apple TV with some tcp sockets.

This was something new for me, I’ve barely touched sockets in any programming language. So I thought, why not use sockets in a language I admire but never worked with (other than Hello World and some math examples)? I choose you, Python!

After some reading and testing I fell in love Python and its syntax. And everything went really smooth. The world of modules to Python is really large. And as the Apple TV uses ZeroConf (Bonjour) I needed to find a nice bonjour module. And so I did: pybonjour.

Usage:
The program it self lets you pass one parameter to the command which is a url to the video you would like to play. It must be a url at the moment. Either a YouTube link (http://www.youtube.com/watch?v=8To-6VIJZRE) or a url to a mp4 video.

To use this you must install two modules to get this running. pybonjour and requests. If you don’t know how to install a Python module. Check this post.

Download the project from GitHub and browse to the folder in the Terminal. E.g.

cd ~/Forks/AirPlayer

Now to get going you only need to type the following:

python airplayer.py http://www.youtube.com/watch?v=8To-6VIJZRE

 

# And the output should be something like:
Simons-MacBook-Air:AirPlayer Simon$ python airplayer.py http://www.youtube.com/watch?v=8To-6VIJZRE
Working...
-----
Available AirPlay Devices
-----
1: Apple-TV
-----
Select your airplay device...
1
Connecting to: Apple-TV

If you pass a link to YouTube the script will automatically pick the highes quality available for the Apple TV and lets you choose which device you want to send the video to. If you download the project there is some lines with comments which, if you uncomment, lets you choose video quality to send to the device.

It will soon be able to send local files with the script as well.

Fork or clone the AirPlayer project from GitHub! I appreciate any collaboration.

How to install Python modules on Mac

First off, you need to install pip. Open the Terminal and type the following

sudo easy_install pip

When pip is installed you can the Python Package Index for modules and libraries to install.

pip install requests

Retheming the blog – going premium for the first time

Today I got bored with the Twenty Elven wordpress theme and thought it was time to get a new one. My first thought was open Photoshop and start design a custom theme, but something made me visit the wordpress section at themeforest.net before. While there I asked myself if it was time to buy a premium theme for wordpress. I know theming in wordpress very well and I know how to design in Photoshop. But all the hustle and time it takes from scratch to a coded design, is it really worth it? I got so many projects besides this blog on my shoulders at the moment, so I picked up Socialike, a tumblog theme, for $30.

The theme has got plenty of features and it was really easy to get started. Though there was one problem, and that is why I’m writing this post. It made me remove a lot of my inspirational content on the blog. My posts with many images looked to bad and it took too much time to layout them the way I wanted, so instead of leaving it be to look all messed up, I deleted them.

I really hope it was worth it. I will start to produce more content here because of this loss.

Lion Update: Stream Mac Desktop to Apple TV

My blog post about Streaming the Mac desktop to a AppleTV has been getting lots of attention. And I’m sorry I haven’t kept up the conversation in the comments.

I’ve been researching this issue today, and the results aren’t very good.
One error was the mediastreamsegmenter, which didn’t come as standard on Lion. The mediastreamsegmenter part is crucial to make this possible. It takes the video output, slices it in segments and streams it.

I did manage to make it start streaming on Lion by downloading the new HTTP Live Streaming package from the Apple Developer site, you need to be registered as a developer to access the files. The problem is it only streams your cursor on a black background.

Apple has unfortunately removed the public API to capture the screen in Lion. And until VLC can find a solution to this, there is no way to use vlclib to stream your Mac desktop to your AppleTV.

How to convert CLLocation to DMS

This snippet lets you convert a CLLocation to a nicely formated string with your latitude and longitude coordinates. Works great on iOS SDK 4 – 5

Input: 57.73201851858309, 12.978544235229492
Output: 57°45’55″N 12°58’42″E

- (NSString *)coordinatesToDMS:(CLLocation *)location {

    NSString *lat = nil;
    NSString *lng = nil;

    // Latititude
    {
        float deg = location.coordinate.latitude;

        NSString *direction = deg >= 0 ? @"N" : @"S";

        float d = floorf(abs(deg));
        float m = (deg - d)*60;
        float s = (m - floor(m)) * 60;

        lat = [NSString stringWithFormat:@"%i°%i'%i\"%@", (int)d, (int)m, (int)s, direction];
    }

    // Longitude
    {
        float deg = location.coordinate.longitude;

        NSString *direction = deg >= 0 ? @"E" : @"W";

        float d = floorf(abs(deg));
        float m = (deg - d)*60;
        float s = (m - floor(m)) * 60;

        lng = [NSString stringWithFormat:@"%i°%i'%i\"%@", (int)d, (int)m, (int)s, direction];
    }

    return [NSString stringWithFormat:@"%@ %@", lat, lng];
}

Load a swf with querystring

I’ve had some problems loading a swf into another swf today.
The problem was solved by moving the whole project onto my local web server running Apache with PHP.


var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete);

var vars:URLVariables = new URLVariables();
vars.name = "Simon";
vars.age = 21;

var req:URLRequest = new URLRequest();
req.data = vars;
req.method = URLRequestMethod.GET;
// Note: The target swf must be on a web server or it wont load your querystring.
req.url = "http://localhost/my/target/swf-file.swf";

loader.load(req);

// In swf-file.swf constructor

var keys:Object = loaderInfo.parameters;
trace(keys['name']); // Simon

Dribbbucket: Embed your Dribble Buckets

My latest project Dribbbucket makes it possible to embed your favorite Bucket from dribbble on your site.

Please visit: Dribbbucket

Frontend
The frontend is JavaScript driven with the major JavaScript Library jQuery and some CSS3.

Backend
As Dribbble doesn’t have a API for their buckets the backend uses Simple HTML Dom Parser to break down the html from the requested Bucket. It makes a JSON file which is stored in “cache” for at least 20 minutes to limit the pressure against Dribbbles servers.

Hope you like it!

Some Great Laptop Bags And Sleeves

I’ve been browsing around the internet and some stores for a while now. It’s really hard to find the perfect bag. There are many good ones out there, but they are hard to find among all the “bad” ones.

I’ve crafted a list of my top contestants in this bag competition. They aren’t in any specific order.

Malcolm Frontier – The Entertainer
The Entertainer is really great looking. It’s 15×13″ and with great padding, which means the it’s a perfect bag for a Macbook Air 13″ without a sleeve. If you still want a sleeve on your laptop it might fit a bit snuggly. There ain’t much space inside this one, the bag is pretty flat. And if you put your charger in the bag it might make some damage to your laptop if you don’t have a sleeve on it.

Walk on Water – Laptop Boarding Bag
The Laptop Boarding Bag from Walk on Water seems really great. It got two compartments, which one is extra padded for the laptop. The other compartment got enough space to fit your charger, mouse pad and Wireless Apple Keyboard, there is also a small pocket inside this compartment which is the perfect size for your mouse. The bag also got two pockets on the outside. This is a well planned laptop bag which gives a lot of bag for the bucks. It comes in a lot of different colors and sizes.

byrd&belle Macbook Air 13″ Wool Sleeve
This sleeve is awesome. I can say that because I’ve got this for my iPad. The 1/8″ thick wool felt makes it really tough. The look of it is really simple, but very stylish and modern. Everything from byrd&belle is made by hand in an ECO-friendly environment.

Freitag F84 Mac Sleeve Air
The Macbook Air sleeve from Freitag is really cool, I really like the urban look of all Freitags bags. I was at my local Apple Premium Reseller and tried this out. It had really great padding and with the tough truck tarpaulins you can’t get a better sleeve. It will prevent damage to your computer if you accidentally drop it. This one felt much better in my hand than my Freitag Nightclub, but that’s another store – you can read about it here if you want.

I’m not yet sure which bags and sleeve I will choose. I’ve got another 5 days before my new Macbook Air arrives. The time is short!