Monday, 11 November 2013

Custom UITableViewCells

I've decided to write a blog post about the best way to create a custom UITableViewCell using a xib. I haven't been able to find a tutorial that covers all the steps, and since this is a very common thing, I'm going to outline them here.

To start, I'm going to assume you have a project set up, and you have a view controller that contains a UITableView.


First thing you are going to do is create the cell.

1) Go File > New > File... > Cocoa Touch > Objective-C class.


2) In the "Subclass of" field, type UITableViewCell. In the "Class" field, type the name of your custom cell. Click next, and make sure to include it in your target.


3) Once the class file has been added, you can add the .xib. Go File > New > File... > User Interface > View. Make sure to name the view the same as your custom cell's class name (for organization purposes).


4) In the .xib, click on the view, and delete it. Then, from the Object Library (on the bottom right), drag a UITableViewCell onto the canvas.


5) Click on the cell in the document tree, then in the Identity Inspector (top right), type your custom cell's class name in the class field. It should autocomplete to the name.


6) In the .xib, click on "File's Owner". In the Identity Inspector, set the class to be your View Controller that contains the UITableView.


7) Now, In Assistant Editor mode, switch the second view to your ViewController.m class. It should be located under Automatic in the file tree. If you don't see it there, try clicking on "File's Owner" (the yellow cube on the left), and try again.



8) Connect the UITableViewCell in the .xib to your viewController class as an IBOutlet (control+drag from the cell to your .m file). Make sure to also import your CustomCell.h in ViewController.m.


9) Customize the UITableViewCell with UILabels, UIImageViews, etc, and make sure to connect everything to CustomCell.h, not the View Controller class. Make sure to add a custom identifier in the Identifier field in the Attributes Inspector. Also, make sure to name your views uniquely. If you were to name your UIImageView "imageView", the results are unexpected since UITableViewCell already has this property.



10) Now, we're going to make a UINib property to hold on to the nib so that we don't have to load it for every cell.



11) Now we're going to implement the two required UITableViewDataSource methods. They should look like this.


Run your app, and you should get something similar to the image below:



Monday, 5 August 2013

Sneak Peek at my new Music app

I'm working on a new music app that runs on iOS 7. Here's a few sneak peek screenshots of the app. 







Sunday, 16 June 2013

FacePic now in the App Store!

The premise of the app is combining pictures from the back and front cameras on your device to create a single image, which can be shared on multiple social networks.


The app has a very simple interface, and responds quite quickly. Look for it in the App Store here.



Saturday, 2 March 2013

AirBoard 2.0 Update

I've completed the 2.0 version of AirBoard. Key features include a new UI, more logic for the second screen display, and support for creating brackets. It's available in the AppStore right now: FULL or FREE.

The bracket creator works by setting up matches, and only advancing the tournament once all the matches have been completed. As you go through each match, completing it, the second screen scrolls to the next unplayed match. Eliminated contestants are stored at the bottom of the tableview.

Here's some screenshots of the app in action:






Sunday, 17 February 2013

AirBoard update coming soon

I've got an update for my scoreboard app AirBoard coming out soon. This new version supports better rankings, and the ability to turn a leader board into a single elimination bracket. I'm currently working on updating the assets, and once that's done I'll be submitting.

Monday, 21 January 2013

Hundreds - Next Riddle (59)

From my sources online, I determined that this riddle was a Vigenère cipher. Vigenère ciphers are similar to Caesar ciphers, except instead of the contacts 3 character shift, it has a shift that is based on a keyword. The keyword is repeated until it is the same length as the text to encode.  i.e. If LEMON was the keyword, you would repeat it like so

LEMONLEMONLEMONLEMON

for a message like

THISISASECRETMESSAGE

Then, you take the location of each letter in the alphabet and add them together to get the new letter (it's explained more complexly on wikipedia, but this is what it boils down to). This would produce the following secret message:

ELUGVDEESPCIFARDWMUR

Thus, to decode one of these, you simply need a keyword, and subtract the keyword's letters from the encoded message's. This is the ruby script I wrote to do this:


file = File.new("vigenere.txt", "r")
cypher = file.gets
file.close

$key = {}
keyword = ARGV[0]


$letterArray = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]

string = ""
count = 0
cypher.each_byte { |i|
   if(i.chr == " ")
      string = string + " "
   else
      keywordLetter = keyword.split('')[count%keyword.length]
      offset = $letterArray.index(keywordLetter)
      index = $letterArray.index(i.chr)
      string = string + $letterArray[(index-offset)%26]
      count+=1
   end
}
puts "\n"+string+"\n\n"

The script takes a single argument, which is the keyword, and tries to decode the message. With that taken care of, all that was left was to guess the keyword. Judging by the previous messages, the keyword had something to do with a harp or a chord or something. I tried pretty much everything I could think of, and then tried RADIUS after looking at the cipher's achievements page. The secret message:

Being part of a bad riddle that nobody cares about.


Tuesday, 8 January 2013

Hundreds - Next Riddle (45)

This riddle looked more like the third riddle, where letter substitution was necessary. With my handy script, I began substituting letters for short words until they made sense. Pretty soon the solution popped out: "What do persistence and a golden harp have in common". Looks like the riddles are related somehow...



Hundreds - Next Riddle (31)

The next riddle wasn't too difficult. I simply went through all the letters one by one and filtered them into columns. Turns out 4 columns is the magic number. Reading down the columns revealed the answer.


Monday, 7 January 2013

Hundreds


Hundreds is a neat new game for iOS where you touch floating circles in an effort to bring their total up to 100. throughout the gameplay, random messages will appear on the screen. These messages are riddles and cyphers which you're expected to solve for the prize of achievements in Game Center. The first one is the only one that's immediately readable: "A mouse has one snout but a hand has five". After touching the screen with 5 fingers, the first riddle was solved. Easy enough. The rest of them, however, aren't even readable.

The second riddle mixed up the letters in each word, so each word had to be unscrambled. There were a few 9 letter words which took a while, but it was eventually solved. "Persistence without patience is a golden harp missing its chord". So these are probably going to require a bit of patience from here on out.

The third riddle (the furthest I've gotten so far) looked like a cypher. Letters looked grouped together alright, but the proper letters were not contained in each word. Being the programmer that I am, I set off  writing a script to help me.

I started off with two arrays of letters; one for the keys and one for the values. As I made a guess at a letter key pair, I would remove them from their respective lists, and then randomize the rest of the pairings every second, printing the potential sentence to the screen.


As you can see, this wasn't going anywhere very fast. But, subbing x for a, o for r, and l for o got me the first four words. Slowly over time, I kept making letter substitutions that seemed logical. The script started to become quite useful in seeing the currently deciphered letters, and possibilities for other letters. Eventually, I reached the end:


No idea what it means, but it works. Only when I went to input the letters into the game did I realize that the cypher was simply the current letter plus 3. So the script was kind of overkill. But it worked!

Below is the next riddle. I will make another post when I solve that one, but it might take me a bit longer...


Here's the ruby script I wrote quickly. Feel free to use it to make or break cyphers.


file = File.new("cypher.txt", "r")
cypher = file.gets
file.close

$key = {}

def randomizeKey()
  keyLetters = ["d","g","h","n","s","t""v","w","y"]
  letters = ["b","j","g","k","q","v","w","y","z"]
 
  $key["x"] = "a"
  $key["k"] = "n"
  $key["q"] = "t"
  $key["e"] = "h"
  $key["b"] = "e"
  $key["o"] = "r"
  $key["l"] = "o"
  $key["f"] = "i"
  $key["c"] = "f"
  $key["r"] = "u"
  $key["z"] = "c"
  $key["m"] = "p"
  $key["p"] = "s"
  $key["i"] = "l"
  $key["j"] = "m"
  $key["u"] = "x"
  $key["a"] = "d"
  $key[""] = ""
 
  while(letters.length > 0)
    keyIndex = rand(keyLetters.length)
    valueIndex = rand(letters.length)
    keyLetter = keyLetters[keyIndex]
    valueLetter = letters[valueIndex]

    $key[keyLetter] = valueLetter

    keyLetters.delete_at(keyIndex)
    letters.delete_at(valueIndex)
  end

end

randomizeKey()

while
  string = ""
  cypher.each_byte { |i|
    if(i.chr == " ")
      string = string + " "
    else
      string = string + $key[i.chr]
    end
  }
  puts string
  randomizeKey()
  sleep(1)
end

Monday, 31 December 2012

1000 Apps Downloaded!

MDF Projects has officially had its 1000th download. The 1000th app was Velocimeter, my speedometer app. It's been a crazy first year of iOS development, and I'm excited to embark on the second one. Also, Happy New Year!

Saturday, 29 December 2012

GKHelper: getting GKPlayer data

I'm going to start going over useful methods from my code in order to help people with similar problems, and give myself a form of self assessment. The first method I'm going to go over is the method I use to get GKPlayer objects from player IDs. This code is from my forthcoming turn based Game Center game (name TBD).

For all my Game Center interactions, I have created a singleton manager, named GKHelper. It handles creating the async calls to Game Center, and processes the information when it comes back. It also has several forms of caching to reduce web requests.

GKHelper has a dictionary which it uses to store GKPlayers by playerID. Whenever a GKPlayer object is fetched, it is first stored in this dictionary before being returned. Whenever a GKPlayer is requested via playerID, the dictionary is checked before the request is made. This logic is handled via the following method:


- (GKPlayer*)playerForPlayerID:(NSString*)playerID {
   if(playerID == nil || [playerID isEqualToString:@""]) {
      return nil;
   }
   if(self.playersDict == nil) {
      self.playersDict = [NSMutableDictionary dictionary];
   }
   
   if([self.playersDict objectForKey:playerID]) {
      return [self.playersDict objectForKey:playerID];
   } else {
      [GKPlayer loadPlayersForIdentifiers:[NSArray arrayWithObject:playerID] withCompletionHandler:^(NSArray *players, NSError *error) {
         
         if (error != nil) {
            NSLog(@"Error retrieving player info: %@", error.localizedDescription);
         } else {
            // Populate players dict
            for (GKPlayer *player in players) {
               [self.playersDict setObject:player forKey:player.playerID];
            }
            [self.delegate performSelectorOnMainThread:@selector(playersLoaded) withObject:nil waitUntilDone:NO];
         }
      }];
   }
   return nil;
}


First, the method checks the playerID to make sure it is valid. Then, it checks the dictionary for the specified player. On the case that it cannot find the player, it begins the async web request for the player data, which it then stores upon return. The delegate is then informed on the main thread (since it may return on a background thread) that the players list has been updated (the delegate usually reloads the data in the tableview). After the web request is sent, the function returns nil indicating the player could not be found. The function will get called a second time upon receiving the player info, and the correct information will be retrieved. This function is costly, however, since the async request takes an array of playerIDs, and we're only passing in a single ID.

That's where the next two functions come in.


- (void)getPlayersForMatch:(GKTurnBasedMatch*)match {
   
   NSMutableArray *identifiers = [NSMutableArray array];
   
   for(GKTurnBasedParticipant *participant in match.participants) {
      if(participant.playerID == nil) {
         continue;
      }
      [identifiers addObject:participant.playerID];
   }
   
   [GKPlayer loadPlayersForIdentifiers:identifiers withCompletionHandler:^(NSArray *players, NSError *error) {
      
      if (error != nil) {
         NSLog(@"Error retrieving player info: %@", error.localizedDescription);
      } else {
         // Populate players dict
         for (GKPlayer *player in players) {
            [self.playersDict setObject:player forKey:player.playerID];
         }
         [self.delegate playersLoaded];
      }
   }];
}

- (void)getPlayersForMatches:(NSArray*)matches {
   NSMutableArray *identifiers = [NSMutableArray array];
   
   for(GKTurnBasedMatch *match in matches) {
      for(GKTurnBasedParticipant *participant in match.participants) {
         if(participant.playerID == nil) {
            continue;
         }
         [identifiers addObject:participant.playerID];
      }
   }
   
   [GKPlayer loadPlayersForIdentifiers:identifiers withCompletionHandler:^(NSArray *players, NSError *error) {
      
      if (error != nil) {
         NSLog(@"Error retrieving player info: %@", error.localizedDescription);
      } else {
         // Populate players dict
         for (GKPlayer *player in players) {
            [self.playersDict setObject:player forKey:player.playerID];
         }
         [self.delegate playersLoaded];
      }
   }];
}


These two functions take a GKTurnBasedMatch, and simply fill up the player dictionary with the GKPlayer objects. The first method I call when a new match is created, and the second method is called when the app is first opened, right after the current matches are loaded. They insure that the loadPlayers method is only called once at app loading, and once per new match. There are a few problems with this setup although.

For one, the latter two methods have no way of communicating with the former method regarding making async web requests. In other words, getPlayersForMatches could be called, immediately followed by playerForPlayerID. This would start two async web requests for the same information, with potentially bad results. There's also the case where the async web request fails to get a certain GKPlayer for a playerID, but still calls the delegate's playersLoaded method. Thus, an infinite loop will be formed.

If I ever get around to optimizing these methods, I'll post the new ones below. Also, make sure to comment if you have any questions. Also, sorry about the code formatting, haven't figured out how to make it format properly yet...

Tuesday, 11 December 2012

Lego Trains are awesome

Found my old Lego train, so I set it up around the tree. Here are some pictures.




AirBoard Overview

AirBoard is a handy app I made to keep score during spontaneous tournaments that sometimes occur. The app is compatible with an Apple Tv, so when you mirror your iOS device's screen on the ATV, it shows the overall scoreboard.

This allows you to edit the list on the fly, and the secondary screen will update once you're finished. I'm planning to add a boatload of features to this app, such as saving a scoreboard, creating a tournament bracket from a scoreboard, and possibly inter-device communication. The UI assets do need an upgrade though...

Another cool feature is the ability to name the scoreboard by typing directly on the navigation bar.

Screenshots below:



Tuesday, 20 November 2012

It's been a while

It's been a long time since I last posted. Since then I've added another app, called Air Board.

Air Board is a handy scoreboard app that takes advantage of linking your iOS device to a secondary screen. When connected to an Apple TV, Air Board will show the top players sorted by score. Updates made on the device are reflected on screen with animations. I'll post some pictures soon.

I'm also working hard on another Game Center based app which will be my best one yet. More info to come.


Tuesday, 11 September 2012

Radians is in the AppStore

Radians has been approved by the AppStore, and is available to download. There's a few updates I want to work on, but the app's in a pretty good state. The download link is below:

Radians

Tuesday, 4 September 2012

Velocimeter available in the AppStore

Velocimeter is a speedometer app I made and uploaded to the AppStore in a few hours. It shows you how fast you're going, and keeps track of stats like top speed and 0 - 100 time. More features will be added when I have time.

Download the app using the link below!

Velocimeter


Tuesday, 28 August 2012

Radians has been submitted to the App Store!

Radians (the name for my map app) has been submitted to the App Store. It's not in a completely stable state, but I think it's ready for public release. There's still several features I want to add in over the months, so look for updates. I haven't yet decided the pricing for the app. Pics below.




Wednesday, 30 May 2012

Mapper Update

I've had some issues with compiling the Mapper app on my devices, so I rewrote the app in a new project. I've also added some pretty sweet features. Hopefully I have a release ready version in a few weeks. Pics to come soon.

Sunday, 15 April 2012

Article Summarization - Update

This is my last project of my undergrad, so I'm going to try and get it finished this week. I've separated the scripts into a reader and a summarizer. One teaches the algorithm, and the other implements it. I'm slowly growing my corpus day to day. I looked into parsing RSS feeds to increase the corpus size, but I decided I could save more time and retain accuracy by doing it manually. I'm aiming for a corpus that involves 100 articles; right now I'm at 40.

I'm planning on testing different corpus sizes and different algorithms against a grading system of my design. Hopefully I can prove that as the corpus grows, the algorithm gets more effective.

More updates to come.

Tuesday, 3 April 2012

Triple Monitors!

I recently purchased an iPad 2 for cheap on eBay, and I've been playing with it for a few days. It's quite fun for games, as well as news with apps like Flipboard.

One of the cooler uses I have for it is as a third monitor for my Air. Using AirDisplay, you can link your iPad as a third monitor. See picture below.