Joris Kluivers

I like to build software.

Launching the Twitter App to a Profile, With Web Fallback

To give users of your iOS app the option to follow you on twitter, you could open the webpage of your app account. Better yet, why not just open the Twitter app itself? Most twitter users probably have the official client installed. Using the custom url scheme supported by the Twitter app, you can display your account profile directly. Users are now only one tap away from following you.

The following URL will open the app on the @app_devs profile page:

1
twitter://user?screen_name=app_devs

A reusable piece of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (BOOL) openTwitterProfile:(NSString *)profileName {
    BOOL didOpenApp = NO;

    UIDevice *device = [UIDevice currentDevice];

    NSString *profileURLString = [NSString stringWithFormat:@"twitter://user?screen_name=%@", profileName];
    didOpenApp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:profileURLString]];

    if (!didOpenOtherApp) {
        // fallback to Safari
        NSString *profileURLString = [NSString stringWithFormat:@"https://twitter.com/%@", profileName];
        didOpenApp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:profileURLString]];
    }

    return didOpenApp;
}