Background Music Queue with AVPlayer and iOS4

Update 2010-Nov-11: Apple has come out with AVQueuePlayer in iOS 4.1, and presumably most people are going to move to the latest version now that it performs well even on the 3G, but the AVPlayer can still be used to manually create a queuing of items, but if you want all the hard work to be done for you, with only a few limitations, then use AVQueuePlayer instead.

Update 2010-Oct-19: Apple has been updating their documentation slightly, and it has more information now with regard to setting up an asset. This is more specifically for video, but it might help fill in the blanks for just audio. AVPlayer Playback Guide

The iOS 4.0 framework introduced AVPlayer which allows playing a MP3s from the iTunes Music Library in the background without using the iPod music player. This has a couple benefits over the MPMusicPlayerController in that you can add new items, stop, or play music in the background. Also, a custom icon can represent your app in the multitasking menu where the ipod controls are located. If you stick with [MPMusicPlayerController iPodMusicPlayer] you can play music in the background through the ipod player, but once an app goes into the background, it no longer has control over the playback of the music.
Continue reading Background Music Queue with AVPlayer and iOS4

How To Securely Manage Your Online Identity

Today we use hundreds of websites on a daily or weekly basis. Many of these require us to create and login with a password. Over time managing all of your identities is a serious challenge. I have decided to give an initial look at the problems and some solutions that you are likely already using to help in this regard.

Problem with Passwords

We’re Human

People use simple passwords that are common words, names, or are patterns of keys such as ‘qwerty’ or ‘asdfasdf’ because they are easier to remember. Many people also use a small number of passwords to login to hundreds of websites that they visit each day. I would also guess that many people use the save password feature with their browser in order to save from typing their passwords in each time they visit a site that requires it. Anyone can walk up to your computer and login to any website for which you have a saved password. So we use simple passwords, and reuse them, and store them in our browsers. How can we prevent ourselves from this inherently insecure behavior?

How secure is your password? Find out.

Continue reading How To Securely Manage Your Online Identity

Random ‘Draw’ With Shuffle

While writing Naval Fight I wanted to have the easy computer opponent select a random position on the board grid every turn.

First I implemented a naive solution creating an array of all possible positions that would be randomly selected from, but after a selection was used it was removed from this array.

validPositions = gridPositions.clone();

// selection
position = validPositions[random() * validPositions.length];

This solution was adequate, but there can always be improvements, and now that I am storing information in a second array, how can it be used more efficiently. Thinking about the problem further it became apparent that this was akin to listening to a playlist on shuffle. Which then gives the light bulb moment that instead of thinking about random selection with no repeats I should just shuffle the array of positions, just like having a deck of cards and removing a card for each turn, noting it’s value (ie: grid position).

After a little searching I came across the Fisher-Yates shuffle algorithm, and found an efficient and unbiased implementation:

// setup
int currentPosition = 0;

for (int i = gridPositions.length; i > 1; i--) {
  // Pick a random element to swap with the i-th element.
  int j = uniform_random() * i;
  // Swap elements.
  int tmp = gridPositions[j];
  gridPositions[j] = items[i-1];
  gridPositions[i-1] = tmp;

// selection on each turn
position = gridPositions[currentPosition++];

This solution generates an array of possible positions which are then shuffled, and finally an index into the array is stored where the next position can be read. Each time the position is accessed from the array this counter increases to always point at the next position. There are other solutions that would work, but this seems to be a good final solution for my problem.