Alex G
Alex G

Reputation: 2309

Integrate twitter into iOS 5 but maintain backwards compatibility with older iOS

Been following this great tutorial on how to integrate twitter into your app. I know there are other ways that programmers have used to integrate twitter before iOS 5 but my question is this:

My app supports iOS 3.0+ so if I integrate twitter using just the iOS 5 way of doing it, how will this affect my users that aren't using iOS 5? Will it even work for them?

Thanks!

Upvotes: 3

Views: 1041

Answers (4)

Scott Densmore
Scott Densmore

Reputation: 1469

Use weak linking and some code like the following:

 - (void)tweet
{
Class tweeterClass = NSClassFromString(@"TWTweetComposeViewController");

if(tweeterClass != nil) {   
    if([TWTweetComposeViewController canSendTweet]) {
        TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
        tweetViewController.completionHandler = ^(TWTweetComposeViewControllerResult result) {
            if(result == TWTweetComposeViewControllerResultDone) {

            }
            [self dismissViewControllerAnimated:YES completion:nil];
        };

        [self presentViewController:tweetViewController animated:YES completion:nil];
    } else {
#if !(TARGET_IPHONE_SIMULATOR)
        [self displayAlert:@"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup."];
#else
        NSString *tweetString = [NSString stringWithFormat:@"http://mobile.twitter.com/home?status=%@%@", [self urlEncode:@"Check out this awesome pic: "] ,[self urlEncode:[_blobTweet.shortUrl absoluteString]]];
        NSURL *tweetURL = [NSURL URLWithString:tweetString];
        if ([[UIApplication sharedApplication] canOpenURL:tweetURL]) { 
            [[UIApplication sharedApplication] openURL:tweetURL]; 
        }
#endif
    }
} else {        
    // no Twitter integration could default to third-party Twitter framework

}
}

@end

Upvotes: 0

MCKapur
MCKapur

Reputation: 9157

The official API framework wouldn't work unfortunately as the twitter app/integration is only available in iOS 5

A good solution is to use ShareKit, a free API that allows you to integrate twitter, facebook and other social network support.

Upvotes: 4

Jan S.
Jan S.

Reputation: 10348

If you are OK by only making Twitter available for iOS 5 users, you can check if Twitter is available with this:

// Don't forget to import Twitter!
#import <Twitter/Twitter.h>
....
if([TWTweetComposeViewController class] != nil) {
    // your code here
}

Also, make sure that when adding the Twitter framework you set it as optional.

Upvotes: 5

Skabber
Skabber

Reputation: 369

You should look into DETweetComposeViewController. We built it just for this purpose. It is an iOS4 compatible re-implementation of the TWTweetComposeViewController.

Upvotes: 2

Related Questions