Adding Pinterest sharing to your iOS app

Update: It looks like this latest version of Pinterest has removed the URL scheme to post directly to the app. I do hope they add it back in with another update.

I’ve been working on a new version of the iOS app, Wedding Dress Look Book by The Knot, and I wanted to add in sharing features for the most popular networks. This a wedding dress browsing and sharing app, it has to be the top three social networks, Facebook, Twitter, and of course, Pinterest. I know that Pinterest doesn’t have a public sharing API yet, but I use there app on my iPhone, and I have a good idea of how it works. For Facebook and Twitter I find out that iOS supports URL Schemes, which allow me to just open an app as I would open Safari. Just change the protocol, http for regular URLs, to the name of the app. I found a site that has a list of all sorts of apps that support this, fb:// for Facebook, twitter:// for the Twitter app etc. Unfortunately they didn’t have Pinterest, but an easy guess of pinterest:// launched the app right away, though I didn’t know of the options, and I still couldn’t find any documentation. I started to think about how the Pin It bookmark works, the URL scheme must be inside that JavaScript file that Pinterest loads onto the site in mobile Safari. After running the file, http://passets-cdn.pinterest.com/js/pinmarklet.js, through a Javascript Beautifier we can see Pinterest developers speak lolcat with plenty of hazSite and hazIOS. That was it!

51
52
53
54
55
56
if (a.v.hazIOS) {
	a.w.setTimeout(function () {
		a.w.location = "pinit12://" + e
	}, 25);
	a.w.location = "http://" + e
} else a.w.open("http://" + e, "pin" + f, a.a.pop)

Pinterest’s URL scheme is pinit12, I’m guessing 12 is a version number. A little more digging around and I came up with a list of working parameters.

URL Scheme: pinit12://pinterest.com/pin/create/bookmarklet/?

  • media: a direct link to the image or I think an encoded image (base64?)
  • url: the URL of the source website where the image was found
  • description: 500 characters max
  • is_video: self describing

Since this code isn’t officially supported it is subject to change, and thankfully there is a way to future proof your app from having a broken link. You are able to check if a URL can be opened without actually opening it in iOS. The trick then is prepping your URL, seeing if it can be opening, and only then displaying the option to share with Pinterest. Using a UIActionSheet, with ARC enabled, this is what I came up with.

int count = 1;
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Email", nil];
 
NSString *post = [NSString stringWithFormat:@"pinit12://pin/create/bookmarklet/?media=%@",@"http://yourdomain.com/yourimage.jpg"];
NSString *escapedStringURL = [post stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:escapedStringURL];
 
if ([[UIApplication sharedApplication] canOpenURL:url])
{
    [sheet addButtonWithTitle:@"Pinterest"];
    ++count;
}
 
[sheet addButtonWithTitle:@"Cancel"];
[sheet setCancelButtonIndex:count];

This way, in case the URL scheme changes from pinit12 in the future it won’t break your application.

It has been added as a URL scheme to http://wiki.akosma.com/IPhone_URL_Schemes, most links seem to hit that listing. Thanks @akosmasoftware!