user3576196
user3576196

Reputation: 275

How to fix this share button?

I'm working on a sprite kit game and I want to integrate a twitter sharing module at the end of gameplay.

This was the BASIC code I tried on an empty scene to test things:

@implementation gameOverScene

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

        self.backgroundColor = [SKColor orangeColor];
    }
    return self;
}

-(void)showTweetSheet {

    //Create an instance of the tweet sheet
    SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

    tweetSheet.completionHandler = ^ (SLComposeViewControllerResult result) {
        switch (result) {
                //the tweet was canceled
            case SLComposeViewControllerResultCancelled:
                break;

                //the user hit send
            case SLComposeViewControllerResultDone:
                break;
        }
    };

    //sets body of the tweet
    [tweetSheet setInitialText:@"testing text"];

    //add an image to the tweet
    if (![tweetSheet addImage:[UIImage imageNamed:@"name.png"]]) {
        NSLog(@"Unable to add the image!");
    }

    //add a URL to the tweet, you can add multiple URLS:
    if (![tweetSheet addURL:[NSURL URLWithString:@"url.com"]]) {
        NSLog(@"Unable to add the URL!");
    }


}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];

    //presents the tweet sheet to the user
        [self presentViewController:tweetSheet animated:NO completion:^{
            NSLog(@"tweet sheet has been presented");
        }];
    }
}

But I keep getting the error "use of undeclared identifier" when trying to present the tweetSheet view controller when the user taps on the scene.

How can I properly integrate the social framework into my project? is it possible on sprite kit?

Upvotes: 0

Views: 130

Answers (1)

ZeMoon
ZeMoon

Reputation: 20274

First of all, since you have created a method, you do not need to present it from the touch delegate. Instead, call the showTweetSheet method.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];

    //presents the tweet sheet to the user
        [self showTweetSheet];
    }
}

You can present it using the following code:

-(void)showTweetSheet
{

    .
    .
    .
    //Your initialisation as before

    [self.view.window.rootViewController presentViewController:tweetSheet animated:YES completion:^{}];

}

Since this is an SKScene, it cannot present a viewController by itself. You need to present the viewController from another viewController, which can be accessed using the self.view.window.rootViewController property.

Upvotes: 1

Related Questions