Category Archives: iOS

These posts are syndicated from my iOS Dev Diary over at pinkstone.co.uk.

The format is slightly different with more code snippets and less explanations. I had originally designed it as a notebook in iOS Development – now you can follow along if you’re interested.

How to create a Twitter Follow button in your iOS App

Much like the Facebook Like button we can create a Twitter Follow button. It’s easy to create a HTML button which we can load into a small UIWebView (about 40 pixels high):

The code for the button is a JavaScript snippet courtesy of Twitter. You can get your customised button code here:
https://about.twitter.com/resources/buttons#follow

We want to intercept the user action of actually following the link our button generates (it would load the Twitter website and ask users to log in) – so let’s place an invisible UIButton over the web view and action that instead:

This code will first try to open the Twitter app and pass in the desired user profile. If it’s not successful it will try to open Safari with the user’s profile link.

Note that our web button will only be displayed if the user is online. Have a fallback image handy if the user is offline.

How to open the Twitter App from your own iOS App

Opening the Twitter App is pretty much the same as opening the Facebook App discussed earlier, just the URL scheme is different.

This is how we open Twitter so people can follow the user (me in this case, versluis). If the call is unsuccessful, we’ll try to open the Twitter page in Safari. And if that doesn’t work, we’ll display a log message:

To find out which other apps you can opened, and to figure out what scheme they follow:

To register a Custom URL Scheme in your own app:

How to open the Facebook App from your own iOS App

Thanks to Custom URL Schemes, iOS Apps can register themselves and be “woken up” by another app (such as yours). Many can even accept parameters if you do.

Opening another app happens via the UIApplication class, and an NSURL as parameter.

Here’s how we open the Facebook App and direct it to open my Pinkstone Pictures page in it:

If the app is not installed nothing happens – which may mean that the app is not installed. As a fallback we’re opening the same page with Safari. If that also doesn’t work, a log message is displayed.

Note that to pass your page into the Facebook App you need your Page or Profile ID. There are several ways to find those:

To find out which other apps you can opened and to figure out which scheme they follow:

To register a Custom URL Scheme in your own app:

How to create a Facebook Like button in your iOS App

Adding a Facebook Like button to a website in HTML is extremely easy, thanks to Facebook’s Developer API. But it’s not as straightforward to implement it in an iOS App.

Thanks to UIWebViews we can create a small web view, perhaps 80 pixels tall, and load the Facebook Like button into it. Here’s how:

Now we’ll see something like this – and it’s even localised:

Screen Shot 2013-12-23 at 15.28.59

Here’s a Like Button Generator that will give you the URL to your own page (enter a URL, select Get Code, then choose URL):

https://developers.facebook.com/docs/plugins/like-button/

The caveat is that it’s a UIWebView, and as such if users click on the Like Button, the “real” Facebook action happens inside your tiny web view (i.e. Facebook.com loads and prompts the user to login or sign up). Not really what we want.

Let’s fix it by simply placing a transparent UIButton on top of your web view, and hook the button up to an action:

Now you can react however you see fit: open a URL in Safari, open the Facebook App, etc.

Another thing of note: since the button will be pulled in from Facebook.com, it will not be displayed when a user is not online. Check via UIWebViewDelegate and if the URL can’t be loaded present an image from your bundle instead.

How to store a BOOL as NSNumber

You can create an NSNumber with a BOOL like this:

It is technically the equivalent of creating an NSNumber with a literal @1 or @0.

To turn the NSNumber back into a BOOL use this:

Any NSNumber that is not zero will give a BOOL of YES when converted this way, including negative numbers.

How to test the size of an NSDictionary (in bytes)

The NSDictionary class doesn’t have a length property that can tell us how much memory is being used for storing the whole lot. Usually we don’t really care how big our variables grow – but if you’re storing that dictionary somewhere with potentially limited space (such as iCloud Key/Value storage), it may well be of interest.

You can however turn the dictionary into data and test its size like this:

Here we setup a loop that generates a dictionary and adds 1001 entries with an NSNumber literal of 47. This will give us the following information:

Size in bytes: 12954 – Entries: 1001

Note that the above only works if your dictionary has “serialisable” data such as NSNumber, NSDate, NSArray, NSDictionary, NSString or NSData (anything that can be written to a .plist file basically) – but it will not work with custom objects.

How to create a UITabBarController in code

Tab Bar Controllers are setup with an array of view controllers. We’ll create those first, then we simply give said array to our tab bar controller.

In this example, this is a subclass of UITabBarController:

Note that I’m also setting the title property of each view controller. This is displayed as the tab bar text and is left blank if not specified. To override this behaviour and/or specify graphics, set the UITabBarItem of each individual view controller.

The method that creates the view controllers is very straightforward: in this example I have a single ViewController in my storyboard (with a Storyboard ID of “PlainViewController”). All we do here is specify a funky background colour to tell them apart:

Note that the tab bar configuration is not set in stone: you can switch out the view controllers on the fly by calling the setViewcontrollers method again, passing in new content.

Here’s a full working demo project that show it all in action:

and the class reference:

How to swap out a store file in Core Data

You can switch out the NSPersistentStore file in your running Core Data app by first adding your new store file, then removing the previous one. Once done you must execute your fetch request again so that the new data is available.

Hot-swapping stores assumes both NSPersistentStore files are based on the same model. It’s like replacing a USB stick.

First let’s pass a reference of the Persistent Store Coordinator into the class you’d like to make the switch in. Based on the Xcode Utility Project, that’s MainViewController. The Core Data stack is already setup in the App Delegate, so we’ll pass a reference here:

Now that our MainViewController can use the coordinator, here’s how we can switch to a new file. I’m calling the current one store1 and the new one store2:

Test this by fetching your data before and after the swap. You don’t have to remove the second store, you can leave it in place if you’d like to fetch data from both stores. I’m not sure though what will happen if you add data – it may end up in both stores.

To check if your store file has changed, you can subscribe to this notification:

  • NSPersistentStoreCoordinatorStoresDidChangeNotification

Here’s an iOS Demo Project that I’ve put together:

How to remove the gloss effect from your App Icon in Xcode 5

In Xcode 4 there was an option that read “Icon already has gloss effect”. This was available in the Project Settings, as well as a Plist Option under Info. YES would remove the gloss effect.

The above option still works in Xcode 5 unless you use an Asset Catalogue.

If you do, here’s where the option is now hidden:

  • click on the .xcasset file
  • select App Icon
  • in the right hand pane, open the Attributes Inspector
  • tick the box that reads “iOS Icon is pre-rendered”

Screen Shot 2013-12-18 at 15.27.09

How to convert a file path into an NSURL (and back)

To convert a file path into an NSURL:

To create a local path from an NSURL:

NSURLs can also be created directly from an NSString:

If you’re ever tried to pass a local path into this method you’ll have noticed that it doesn’t work. Use the above methods instead.

You can also add path components to a URL, for example to reference your Documents directory:

will give something like

NSURLs have the same method called URLByAppendingPathComponent: