Tag Archives: Social Features

How to open the Pinterest App from your own iOS App

Pinterest_Badge_RedYou can open Pinterest links from your iOS app directly in the Pinterest app if it’s installed. This includes direct links to a user, a user’s board, or a specific pin.

In the following code snippets we’ll do just that, and if the Pinterest App is not installed on the device we’ll open the link in Safari instead. These methods can be hooked up to a UIButton.

Note that for this to work, users need to be logged in to the Pinterest App – otherwise the app opens with a login prompt and not the link we want.

Opening a Pinterest User Profile

Much like on Twitter, users on Pinterest have handles. Mine is versluis2000, and my Pinterest URL is http://www.pinterest.com/versluis2000. Here’s how you open this user via the Pinterest app:

- (IBAction)openPinterestUser:(id)sender {
    
    // open the Pinterest App if available
    if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"pinterest://user/juliav1"]]) {
        
        // opening the app didn't work - let's open Safari
        if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.pinterest.com/versluis2000/"]]) {
            
            // nothing works - perhaps we're not online
            NSLog(@"Dang!");
        }
    }
}

Trying to display your own Pinterest User Profile may crash the Pinterest App – at least that’s what happened in my tests.

Opening a Pinterest Board

Users on Pinterest can pin links to their boards which work like collections or categories. Those boards have slugs that appear after the user handle. I have a board called iOS Projects which has the following URL: http://www.pinterest.com/versluis2000/ios-projects/

Here’s how we can open one directly in the Pinterest App:

- (IBAction)openPinterestBoard:(id)sender {
    
    // open the Pinterest App if available
    if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"pinterest://board/versluis2000/ios-projects"]]) {
        
        // opening the app didn't work - let's open Safari
        if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.pinterest.com/versluis2000/ios-projects/"]]) {
            
            // nothing works - perhaps we're not online
            NSLog(@"Dang!");
        }
    }
}

Opening a Pinterest Pin

When a user finds a link on the web and pins it, the link will receive a unique numeric ID. Such an ID is independent from the user who pins it. Here’s a pin to a sketch of a Polaroid camera I drew: http://www.pinterest.com/pin/76279787413881109/

Let’s open this pin in the Pinterest App:

- (IBAction)openPinterestPin:(id)sender {
    
    // open the Pinterest App if available
    if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"pinterest://pin/76279787413881109"]]) {
        
        // opening the app didn't work - let's open Safari
        if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.pinterest.com/pin/76279787413881109/"]]) {
            
            // nothing works - perhaps we're not online
            NSLog(@"Dang!");
        }
    }
}

How about pinning something directly from my own app?

If you’d like users to pin something directly from your app, take a look at the iOS Pin It SDK – brought to you by the friendly folks at Pinterest. The article also explains the deep linking structure we’re using in the above examples:

Care for a Demo Project? https://github.com/versluis/Pinterest





How to open the Google Plus App from your own iOS App

Google+To open a URL inside the Google+ App we need to know the Google+ URI scheme.

Instead of http:// it’s gplus:// – so if your Google+ URL in a web browser is http://plus.google.com/+WpguruTv, then you can open the same URL from your iOS App with gplus://plus.google.com/+WpguruTv

Let’s have a look at how to do this in code:

- (void)openGooglePlus {
    
    if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"gplus://plus.google.com/+WpguruTv"]]) {
        
        // opening the app didn't work - let's try in Safair
        if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://http://plus.google.com/+WpguruTv"]]) {
            
            // nothing works - go watch TV
        }
    }
}

First we try to open the Google+ App using the gplus:// URI. If that’s unsuccessful we’ll open the standard http:// URI which will automatically bring up Safari.

It’s a good idea to test for network connectivity before attempting to open any URL (for example with the excellent Reachability by Tony Million).





Links to the latest Social Icons

InBug-60px-RFacebook, Twitter & Co. change their brandings every now and again, and every time I need them for a project I keep scrambling to find the official links. Well no more – here’s a list of them all:





How to open the LinkedIn App from your own iOS App

Here’s a sample action that tries to open a LinkedIn Profile with the LinkedIn App. If it’s not installed, it opens a link to the profile in Safari. If that doesn’t work it gives a log message (please handle appropriately in your own code):

- (IBAction)openLinkedIn:(id)sender {
    
    // open the LinkedIn App
    if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"linkedin://#profile/35932112"]]) {
        
        // opening the app didn't work - let's open Safari
        if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.linkedin.com/company/pinkstone-pictures"]]) {
            
            // nothing works - perhaps we're not onlye
            NSLog(@"LinkedIn doesn't works. Punt.");
        }
    }
}

Note that currently you can only link to personal profiles, not to company profiles.

To create a dynamic web button with follower count (or not), check out the LinkedIn Developer Portal and load the code into a small UIWebView.

At the time of writing it appears that none of the LinkedIn provided plugins are actually working anywhere other than the preview. Looks like we’ll have to use a standard graphic instead until this is fixed.





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):

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // add Twitter Follow button in a web view
    NSString *twitterFollowButton = @"<a href='https://twitter.com/versluis' class='twitter-follow-button' data-show-count='true' data-size='large'>Follow @versluis</a><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>";
    
    [self.twitterWebView loadHTMLString:twitterFollowButton baseURL:nil];
}

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:

- (IBAction)twitterButton:(id)sender {
    
    // open the Twitter App
    if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"twitter://user?screen_name=versluis"]]) {
        
        // opening the app didn't work - let's open Safari
        if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://twitter.com/versluis"]]) {
            
            // nothing works - perhaps we're not onlye
            NSLog(@"Nothing works. Punt.");
        }
    }
}

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:

- (IBAction)openTwitter:(id)sender {
    
    // open the Twitter App
    if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"twitter://user?screen_name=versluis"]]) {
        
        // opening the app didn't work - let's open Safari
        if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://twitter.com/versluis"]]) {
            
            // nothing works - perhaps we're not onlye
            NSLog(@"Nothing works. Punt.");
        }
    }
}

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: