Monthly Archives: April 2013

How to create an NSDate object

The easiest way to create an NSDate object is to create “right now” with our convenience method date:

But if you want to create a date object with a date such as your birthday it gets a little bit trickier, and – more importantly – much less obvious.

To do this, we need to create an NSDateFormatter, tell the formatter how to expect the date, and then use its convenience method dateFromString to create the date:

Notice that I tell the date formatter to expect the date as YYYY-MM-DD. I could also have told it to expect it as YYMMDD and then pass @”12-12-12″ in the dateFromString method.

Whichever way you do it, the date format must match your string, otherwise the method returns null.

How to display the full month from an NSDate (such as “February”)

There was me thinking I’d have to whip out NSDateComponents and NSCalendar – but my mind works too complicated at times.

All we need to do this is our good friend the NSDateFormatter and the Unicode Date Format MMMM – let’s check it out:

The MMMM tells the date formatter to display the full month, no matter how many letters it has. You can also use:

  • M for the month number (1 for January or 12 for December)
  • MM for the two-digit month number (01 for January)
  • MMM for the abbreviated month number (Jan for January)
  • MMMM for the full month number (such as January)
  • MMMMM for just the first letter (J for January… not sure who would need this though)

The Unicode Date Format can do amazing things with all aspects of times and dates – check out the complete compendium here:

How to create Linkshare Affiliate Links to iTunes Products

One of the worst ever interfaces i have ever come across is that of Rakuten LinkShare. Even before they’ve been bought by Japanese giant Rakuten it was bad, and it hasn’t been improved since I’ve signed up with them in 2008.

Lucky for us we don’t have to use their interface to get links to iTunes Products – we can use the standard good looking and well working iTunes Link Maker. All we need is go there via LinkShare, which will embed our affiliate code.

Here’s how to do this step by step:

1.) Login to LinksShare and see the awful Home Screen
2.) Head over to Links – Get Links
3.) Click on “US iTunes, App Store, iBookstore, and Mac App Store” (you need to be subscribed and approved for this option to become available).

This should get you to a screen similar to this:


Screen Shot 2013-04-27 at 14.09.41


4.) Notice the grey button that says “Link Maker Tool”. Click that and you get to a new window and is the familiar iTunes Link Builder, as provided by Apple:


Screen Shot 2013-04-27 at 14.12.01


Note the message that reads: “Your affiliate Network is…” – this is the confirmation that you’re building links as an affiliate. If that message isn’t there, your links are not tracked. Instead if LinkShare, this can be a different partner (TradeDoubler is another one for example).

Now you can enter a product and grab the link, which will already contain your affiliate ID.

How to create a UIBarButtonItem in code and make it call a method

Some things are really easy to do via a Storyboard – but when you want to create the same thing in code I frequently forget how to do it.

Turns out it’s equally simple: this example assumes you have a View Controller which is embedded in a Navigation Controller (so it already has a UINavigationBar at the top). Here’s how you add a button to it, set the title and target, and tell it what to do when it’s pressed:

You can set the button on the left hand site by using self.navigationItem.leftBarButtonItem

How to remove a Core Data Model Version

Core Data can handle several versions of your Model. It’s easy to add a version (via Editor – Add Model Version) and set it active, but it’s not so easy to remove a version you no longer need.

Thankfully there is a way to delete version files which goes a long way to declutter your brain. The secret lies in the fact that the .xcdatamodeld file is actually a Package and can contain more than one file. It’s like the .app extension which isn’t just one file. I never knew this!

To explore, select your versioned .xcdatamodeld file, right-click it and select “Show in Finder”. Once in there right-click it again and select “Show Package Contents”. Surprise – here are all your Model Versions. Before you go on a mad deleting spree, head back to Xcode and do the following:

  1. activate a Model Version that you want to keep (by going to the top level .xcdatamodeld entry, then select it under “Versioned Core Data Model”)
  2. next remove the entire file from your project (just the reference… do not move to trash)
  3. head back to the Finder and delete the files inside the Package you no longer want
  4. go back to Xcode and add the .xcdatamodeld file again

Voila – now all your unnecessary Model Versions are gone. Perhaps in a future version of Xcode there will be an easier way to do this, but as of Xcode 4.6.2 (April 2013) there is not.

Further Reading

Thanks to David Avendasora for this wonderfully simple explanation on Stack Overflow:

How to create a View Controller defined in you your Storyboard programmatically

Your View Controllers are created by the Storyboard automatically depending their defined relationships in Interface Builder.

Sometimes however we need to create and transition to View Controllers we’ve defined in code. For example, if you want to transition to a view as part of displaying a search result.

We can do this by creating a new UIStoryboard object and then asking it to create a View Controller defined in it. For this to work you need to give your View Controller a unique identifier using the Identity Inspector (under Identity, set a Storyboard ID).

This example assumes we have a Storyboard file called MainStoryboard.storyboard in which there’s a View Controller called DetailView. Here’s how we create it and push it onto a stack of Navigation Controllers:

Note that you define the Storyboard without its extension. If the bundle parameter is nil (as it is here) then the compiler assumes your main bundle.

How to add a Search Display Controller to a UITableView (in code)

We’ve recently discussed how to deal with a Search Bar and Search Display Controller using Interface Builder. You can however do this in code too. This can be useful if you don’t want to make all the relevant connections in every Storyboard.

This example assumes you have a Table View (self.tableView) to which you’d like to add a Search Bar and Search Display Controller at the top. We do this by utilising the Table View’s tableHeaderView property:

Your View Controller also needs to conform to the UISearchBarDelegate Protocol for this to work properly.

How to change your User Name in WordPress

This week has seen a rather large scale attack of a bot network that specifically targets the “admin” user in WordPress installations. This means that if your site as a user called “admin” then your site is under threat of being a potential target.

No matter if you’re using “admin” as an active user or not: if it exists, your site is at risk. Period.

In this podcast I’ll show you how to change your user name. Sadly this isn’t as easy as just amending “admin” to something else; you’ll have to create a new user, then delete the “admin” user from your site. Don’t be afraid though, I’ll cover each and every step in detail and explain why this is necessary.


Further Reading

Thanks to Richard Bagnall for bringing this to my attention 😉

How to add a System User Account on Linux (CentOS)

CentOSOn both CentOS and Red Hat Linux systems you can easily create new user accounts with their own home directories.

Here’s how you do this from the command line. I’m assuming you’re logged in as root, and the new user we’d like to create is called “testuser”:

Let’s give our testuser a password so s/he can login to the system via SSH:

This will prompt for a new password (twice), followed by the message that “all authentication tokens have been updated”. Which is nice.

You can now login as “testuser” with your designated password. Testuser’s home directory is located here:

That’s all there’s to it – congratulations on creating a new user on your system 😉

Removing a User Account

If you’re done with the account you can remove it with

The data in this user’s home directory will still be intact, even when the user is removed. In case you’d like to do some house cleaning, you can pass -r which will remove the user’s directory and all contents he may have had:

How to parse a JSON URL in iOS and Cocoa

Both Cocoa and iOS have built-in methods to parse (and generate) JSON data using NSJSONSerialization.

Most commonly you’ll want to call a URL with parameters which will return some data. You take this output and tell Objective-C that it’s JSON, then typecast the return to an array or a dictionary which in turn lets you access the data. It’s genius!

Here’s an example:

What you’ll get back depends on your individual JSON structure of course. This can be quite complex, for example you could receive an array full of dictionaries, each of which could be full of dictionaries. JSON data can be mapped to an NSArray, NSDictionary, NSNumber and NSNull.

Once you have access to the top level elements you can then loop through each one, examining what’s in it and access what you need.

Further Reading