简体   繁体   中英

Is it possible to have a Twitter / Facebook share button in a sprite-kit game?

I'm completely lost, all the tutorials I've found have been for iOS 6 apps with Storyboards. My question is if you can have a twitter or facebook sharing feature in a sprite-kit game? If so what is the best way to go about adding it? Alot of the tutorials I've read over use viewcontrollers but sprite-kit uses scenes so I'm a bit confused.

Thanks.

Here is an answer for twitter. Your Welcome!

1) import the Social framework under the Build Phases tab then Link Binary with Libraries. Put this code at the top of your SKScene that you want to share it in.

#import <Social/Social.h>   

2) add your tweet button in the -(id)initWithSize:(CGSize)size method with this code:

SKSpriteNode *tweetButton = [SKSpriteNode spriteNodeWithImageNamed:@"share"];
tweetButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)-150);
tweetButton.name = @"tweet";
[self addChild:tweetButton];

3) in your touchesBegan: method put the following code to handle clicking the tweet button and composing a tweet

if ([node.name isEqualToString:@"tweet"]) {
    //  Create an instance of the Tweet Sheet
    SLComposeViewController *tweetSheet = [SLComposeViewController
                                           composeViewControllerForServiceType:
                                           SLServiceTypeTwitter];

    // Sets the completion handler.  Note that we don't know which thread the
    // block will be called on, so we need to ensure that any required UI
    // updates occur on the main queue
    tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
        switch(result) {
                //  This means the user cancelled without sending the Tweet
            case SLComposeViewControllerResultCancelled:
                break;
                //  This means the user hit 'Send'
            case SLComposeViewControllerResultDone:
                break;
        }
    };

    //  Set the initial body of the Tweet
    [tweetSheet setInitialText:@"This is my tweet text!"];

    //  Adds an image to the Tweet.  Image named image.png
    if (![tweetSheet addImage:[UIImage imageNamed:@"image.png"]]) {
        NSLog(@"Error: Unable to add image");
    }

    //  Add an URL to the Tweet.  You can add multiple URLs.
    if (![tweetSheet addURL:[NSURL URLWithString:@"http://twitter.com/"]]){
        NSLog(@"Error: Unable to URL");
    }
    UIViewController *controller = self.view.window.rootViewController;
    [controller presentViewController:tweetSheet animated: YES completion:nil]; 
}

This may help. ShareKit

It is an easy way to integrate Facebook, Twitter, and other social networking services into your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM