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; }
Categories: dev, Mobile, Tech
cocoa, ios, ios 5, iphone, mobile dev, objective-c, twitter, TWTweetComposeViewController
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]) {…}
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.
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.
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?
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.
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.
Sure, because it gets weak-linked, but NSClassFromString(@”TWTweetComposeViewController”), and possibly [TWTweetComposeViewController class], ought to return nil.
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.
[TWTweetComposeViewController canSendTweet] would return 0 for a non-available, weak-linked class, which equals false / NO.
So we should work out fine since nil and NO evaluate the same. Thanks for the great discussion!
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.
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
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.
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.