Home > dev, Mobile, Tech > iOS 5 Twitter SDK Available

iOS 5 Twitter SDK Available

I could not find anyway online for testing if the user had the Twitter API available (IE: they’re running iOS 5). The best solution I came up with was to instantiate the TWTweetComposeViewController and check if it is nil. If so then I don’t include any references to Twitter in my app.

-(BOOL) twitterSDKIsAvailable{
    TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
    BOOL twitterSDKAvailable = tweetViewController != nil;
    if(tweetViewController != nil){
         [tweetViewController release];
    }

    return twitterSDKAvailable;
}
  1. Matt Dolan
    October 13, 2011 at 6:53 pm

    Your best bet would probably be something like the following:

    if (NSClassFromString(@”TWTweetComposeViewController”) && [TWTweetComposeViewController canSendTweet]) {…}

    It should work although I’ve not tested it, only just starting to look at the Twitter API myself… I use the same technique for detecting the availability of AirPrint functionality and it works like a charm as follows:

    if (NSClassFromString(@”UIPrintInteractionController”) && [UIPrintInteractionController isPrintingAvailable]) {…}

    • October 14, 2011 at 7:11 am

      Thanks for the tip, Matt. The only reason I chose not to go that route is because even if the user is on a pre-iOS 5 OS the TWTweetComposeViewController class is still there. I see that you check is “isPrintingAvailable” in your AirPrint example; however, “canSendTweet” doesn’t do exactly what you would think it would do — if the user hasn’t setup their Twitter account yet (and they’re running iOS 5) this method will return “NO”. I don’t want to do that since the phone will prompt them that they haven’t setup Twitter yet and would they like to now. This is why I have two separate methods, one for detecting if the phone supports Twitter and one that detects if they’ve configured Twitter.

      • Matt Dolan
        October 14, 2011 at 10:25 am

        Strange, didn’t realise that would be the case with the TWTweetComposeViewController. I use the canSendTweet in my check more because I want it to fallback to a different implementation – I’m not sure I’m that keen on the Apple way as you cannot initialise it with more than 140 characters. I’m using the canSendTweet as a way for users of my apps to use the original implementation.

      • October 14, 2011 at 10:53 am

        So if they have configured native Twitter you allow them to use it, otherwise you don’t give them a chance to configure it and fallback to a custom implementation?

  2. Steven Kramer
    November 8, 2011 at 1:08 pm

    TWTweetComposeViewController available on pre-iOS 5? Have you tested? Because that would suggest Twitter.framework was already included in earlier iOS versions. Can’t imagine that.

    • November 8, 2011 at 9:40 pm

      Steven,

      If you compile with iOS5 and your app targets prior to iOS 4 then the compiler handles this for you. I tested this out by compiling the app with the iOS 5 SDK and then deploying to an iOS 4 device and it compiled and ran (sans the Twitter integration) without any issues.

  3. Steven Kramer
    November 9, 2011 at 5:44 am

    Sure, because it gets weak-linked, but NSClassFromString(@”TWTweetComposeViewController”), and possibly [TWTweetComposeViewController class], ought to return nil.

    • November 12, 2011 at 11:31 pm

      I definitely understand the weak-linking part, but I’m still scratching my head on why it’s not causing exceptions on pre iOS 5 OSes.

      • Steven Kramer
        November 14, 2011 at 9:15 am

        [TWTweetComposeViewController canSendTweet] would return 0 for a non-available, weak-linked class, which equals false / NO.

  4. November 14, 2011 at 11:49 am

    So we should work out fine since nil and NO evaluate the same. Thanks for the great discussion!

  5. Gyanendra Kumar
    December 7, 2011 at 6:08 am

    Hi briananto,

    Hope you are doing well. I am trying to integrate twitter plugin. I am unable to do it. It crashes the application [tweetViewController setCompletionHandler:^(TWTweetComposeViewControllerResult res) after this line i am unable to fine what causing the problem. One time i got an error saying that Attempting to create USE_BLOCK_IN_FRAME variable with block that isn’t in the frame. I don’t know what is the error. Please help me out. I am using phonegap1.1.0 and xcode4.5.

  6. December 7, 2011 at 10:32 am

    Gyanendra,

    This issue is because you are probably weak-linking to a certain library. Remove any weak links, you can find more information here: http://stackoverflow.com/questions/6738858/use-of-blocks-crashes-app-in-iphone-simulator-4-3-xcode-4-2-and-4-0-2

    Also, since you are using PhoneGap I have released an official PhoneGap plugin which you can grab from Github: https://github.com/phonegap/phonegap-plugins/tree/master/iPhone/Twitter

  7. July 17, 2013 at 2:16 pm

    Heya i am for the first time here. I came across this
    board and I to find It truly helpful & it helped me out a lot.
    I am hoping to present one thing again and aid others like
    you helped me.

  8. July 28, 2013 at 12:24 am

    Just wish to say your article is as astonishing.
    The clearness in your post is just great and i can assume you’re an expert on this subject. Well with your permission let me to grab your feed to keep up to date with forthcoming post. Thanks a million and please carry on the gratifying work.

  1. No trackbacks yet.

Leave a reply to brianantonelli Cancel reply