Twitter – ShiVa Engine


vklymenko: “This integration works for iOS 5+. It requires the player to log into his twitter account manually in the device settings.”

ShiVa Steps

Add a handler onShareToTwitter with a string parameter – it will be the pre-defined text that player can later edit before post. Example:

MainAI.onShareToTwitter ( s )

XCode Steps

After adding the Twitter framework, do the following code alterations:


On top:

 #import "Twitter/Twitter.h"

After AdMobViewDelegate *pAdMobViewDelegate ;

UIViewController               *twitterVC;

Before @end:

- (void)postToTwitter;


Inside – (void)drawView, somewhere here:
if ( bOverlayMovieIsPlaying ) { … } else { … here … }

if (S3DEngine_Twitter_GetWantPost())
    [self postToTwitter];

Next func should be added before “-(void)showVirtualKeyboard”

- (void) postToTwitter {
    NSString- sText = [[NSString alloc] initWithUTF8String:S3DEngine_Twitter_GetTweetText()];
    TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
    [twitter setInitialText:sText];
    [twitter addURL:[NSURL URLWithString:[NSString stringWithFormat:@""]]];
    if ([TWTweetComposeViewController canSendTweet]) {
        twitterVC = [[UIViewController alloc] init];
        UIWindow- window = [UIApplication sharedApplication].keyWindow;
        [window addSubview:twitterVC.view]; //Doesn't show up without this
        [twitterVC presentModalViewController:twitter animated:YES];
    } else {
        UIAlertView- alertView = [[UIAlertView alloc] initWithTitle:@"Unable to tweet"
                message:@"Error: No connection, no Twitter in your iOS settings or iOS < version 5"
                delegate:self cancelButtonTitle:@"Dismiss"
        [alertView show];
        [alertView release];
    twitter.completionHandler = ^(TWTweetComposeViewControllerResult res){
        if (res == TWTweetComposeViewControllerResultDone) {
            UIAlertView- alertView = [[UIAlertView alloc] initWithTitle:@"Tweet Successful!"
                    message:@"You successfully tweeted"
                    delegate:self cancelButtonTitle:@"OK"
            [alertView show];
            [alertView release];
        } else if (res == TWTweetComposeViewControllerResultCancelled) {
            UIAlertView- alertView = [[UIAlertView alloc] initWithTitle:@"Something wrong"
                    message:@"Tweet failed to send, try again later"
            [alertView show];
            [alertView release];
        [twitter release];
        [twitterVC dismissViewControllerAnimated:YES completion:^(void) {
            [twitterVC.view removeFromSuperview];
            [twitterVC release];

You need to replace "" with your link, or if you don't need it (share link together with text) - just delete this line. It is also possible to add an image here.


extern bool S3DEngine_Twitter_GetWantPost();
extern void S3DEngine_Twitter_SetWantPostFalse();
extern const char *S3DEngine_Twitter_GetTweetText();


On top:

static bool bWantShareTwitter = false;
static char sTwitterText[64] = "";

Before "extern "C" bool S3DEngine_iPhone_LoadPack ( const char *_pStartupPackPath, const char *_pPackPath":

void onShareTwitterCallback ( unsigned char _iArgumentCount, const void *_pArguments, void *_pUserData ) {
    bWantShareTwitter = true;
    if ( _pArguments && ( _iArgumentCount == 1 ) ) {
        const S3DX::AIVariable *pVariables = (const S3DX::AIVariable *)_pArguments ;
        if ( pVariables[0].GetType ( ) == S3DX::AIVariable::eTypeString )
            strncpy ( sTwitterText, pVariables[0].GetStringValue ( ), 63 ) ;

Inside "extern "C" bool S3DEngine_iPhone_LoadPack":

S3DClient_InstallCurrentUserEventHook   ( "MainAI", "onShareToTwitter",  onShareTwitterCallback, NULL ) ; 

and somewhere at the end of this file:

extern "C" bool S3DEngine_Twitter_GetWantPost() {
    return bWantShareTwitter; }
extern "C" void S3DEngine_Twitter_SetWantPostFalse() {
    bWantShareTwitter = false; }
extern "C" const char *S3DEngine_Twitter_GetTweetText() {
    return sTwitterText; }

so its about it : ) Lets discuss the "login" feature... Facebook is using web-kind login, but keep player inside the game (returning him to the game after "post to wall" done) etc. Let me know also if I can answer any questions regarding this implementation.


Most code in this tutorial comes from this video:
[video_lightbox_youtube video_id="PtCtcx6CgOA" width="1024" height="600" auto_thumb="1"]

Need more answers?

  • slackBanner