Twitter integration with iOS 6, weak linking and device checking

So I have been working on an update to my iOS game, Cosmosis. One of the new features I am adding (partly due to me having to remove the old OpenFeint framework, is a tweet button on the game over scene.

There are actually many different choices, from the Twitter framework available in iOS 5 to Sharekit, to the newer Social framework available in iOS 6.

I decided to go for a quick and easy win with the Social framework, however the drawback here is that it requires iOS 6 on the device. If you try to build and run the application on anything less than this, the app will immediately crash and you’ll see a reference to the framework issue and a missing image in the crash logs.

So the trick here is to first of all weak link the framework. This is as simple as changing the Social framework from “required” to “optional”. Next you need to put in some code to check that the user has iOS 6 or above and only allow the tweet action to occur if this is the case. If not the case (i.e. less than iOS 6), then I chose to display a UIAlertView notifying the user that they need iOS 6 in order to use Twitter integration.

weak-link-social-framework
Weak linking the social framework.

 

uialertview-no-social-framework
UIAlert displayed when the Social framework is not supported.

To check the iOS version on the device running the game, I found a nice solution using preprocessor macros on stackexchange.

Add the following to the top of your header (.h) file:

[sourcecode language=”objc”]
/*
* System Versioning Preprocessor Macros
*/

#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)</code>
[/sourcecode]

Then in my Tweet method’s code which I use in my implementation file (.m), I simply do:

[sourcecode language=”objc”]
if (SYSTEM_VERSION_LESS_THAN(@"6.0")) {

UIAlertView *alertView = [[UIAlertView alloc]

initWithTitle:@"Sorry"

message:@"Twitter integration is only supported on iOS 6 and above. Upgrade to iOS 6 to enable this feature."

delegate:self

cancelButtonTitle:@"OK"

otherButtonTitles:nil];

[alertView show];

}

else

{

//code to tweet goes here

}

[/sourcecode]

Leave a Comment