Tag Archives: Social Features

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:

[emember_protected]

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:

[/emember_protected]

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 share items with a UIActivityViewController

Another option to share items in your app is to bring up a dialogue that contains several options, like the Photos app does:

Screen Shot 2013-12-06 at 18.54.19

This is done with a UIActivityViewController and requires minimal work on our part. This controller is very clever and only brings up relevant services (you can’t print a video, or you can’t share a video to Twitter, so those options don’t appear in the list).

Here’s how we use it:

[emember_protected]

UIActivityViewController does not require any frameworks to be imported into your class. The items array can take anything from text, pictures or video (just not mixed together). If an item is not setup the icon won’t appear in the list.

You can specify which items to exclude from the list by defining an array of UIActivities, and you can specify a completion block.

[/emember_protected]

How to post to Facebook and Twitter using Social Framework

It is extremely simple to bring up Facebook and Twitter Sheets in your app using the Social Framework.

Once imported you can create a new instance of SLComposeViewController, initialise it with text, an image and a URL, and present it modally from your own view controller.

Screen Shot 2013-12-06 at 18.50.57

You don’t have to specify anything other than which service you’d like to use, in which case the presented sheet will be blank. Here we bring up a tweet sheet (i.e. Post to Twitter dialogue):

And here we specify all three values for a post to Facebook:

Available service types are

– SLServiceTypeFacebook
– SLServiceTypeTwitter
– SLServiceTypeSinaWeibo
– SLServiceTypeTencentWeibo

You can test if a service is available using the isAvailableForService method (which always returns YES on the simulator, and only gives accurate results on a real device):