ios - 是否可以在 sprite-kit 游戏中使用 Twitter/Facebook 分享按钮?

标签 ios objective-c facebook twitter sprite-kit

我完全迷路了,我找到的所有教程都是针对带有 Storyboard 的 iOS 6 应用程序的。我的问题是您是否可以在 sprite-kit 游戏中拥有 Twitter 或 Facebook 共享功能?如果是这样,添加它的最佳方法是什么?我读过的很多教程都使用 viewcontrollers,但 sprite-kit 使用场景,所以我有点困惑。

谢谢。

最佳答案

这是推特的答案。欢迎您!

1) 在 Build Phases 选项卡下导入 Social framework,然后 Link Binary with Libraries。将此代码放在您要共享的 SKScene 的顶部。

#import <Social/Social.h>   

2) 使用以下代码在 -(id)initWithSize:(CGSize)size 方法中添加您的推文按钮:

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

3) 在您的 touchesBegan: 方法中放入以下代码来处理点击推文按钮和撰写推文

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]; 
}

关于ios - 是否可以在 sprite-kit 游戏中使用 Twitter/Facebook 分享按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23460212/

相关文章:

ios - Xcode:标签制作错误

ios - iOS 内存崩溃上的 Web Audio API

objective-c - 为什么 Objective-C 编译器需要知道方法签名?

ios - Apple Pay 钱包屏幕 (PKAddPaymentPassViewController) 在我的应用程序中未正常显示

ios - Facebook SDK 4.0 - currentAccessToken 在 loginButton didCompleteWithResult 中仍然为零

facebook - FB.ui 发送对话框 500 错误...但仅在指向我的网站的链接上?

iOS通知观察者单元测试( objective-c )

ios - xcode 在上传期间在错误的路径引用 itmsTransporter

iPhone-避免在 UIImagePickerController 中进行视频压缩

android - 如何使用 Facebook Android SDK 向其他 friend 的留言墙发送消息/分享对象?