是否有可能在精灵套件游戏中设置Twitter / Facebook分享按钮?



我完全迷路了,我找到的所有教程都是针对带有故事板的iOS 6应用程序的。我的问题是,你是否可以在sprite-kit游戏中加入twitter或facebook分享功能?如果是的话,添加它的最好方法是什么?我读过的很多教程都使用视图控制器,但sprite-kit使用场景,所以我有点困惑。

谢谢。

这是一个关于twitter的答案。你的欢迎!

1)在Build Phases选项卡下导入Social框架,然后将Binary与库链接。把这段代码放在你想要共享的SKScene的顶部。

#import <Social/Social.h>   

2)在-(id)initWithSize:(CGSize)size方法中添加tweet按钮,代码如下:

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

3)在您的touchesBegan:方法中放入以下代码来处理点击tweet按钮并编写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]; 
}

这可能有帮助。ShareKit

将Facebook、Twitter和其他社交网络服务集成到你的应用程序中是一种简单的方法。

最新更新