ios - 如何修复此共享按钮?

标签 ios objective-c twitter sprite-kit

我正在开发一款 Sprite 套件游戏,我想在游戏结束时集成一个 Twitter 共享模块。

这是我在空场景中尝试测试的基本代码:

@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");
        }];
    }
}

但是当用户点击场景时尝试呈现 tweetSheet View Controller 时,我不断收到错误“使用未声明的标识符”。

如何正确地将社交框架集成到我的项目中? Sprite 套件上可以吗?

最佳答案

首先,由于您已经创建了一个方法,因此不需要从触摸委托(delegate)中呈现它。相反,请调用 showTweetSheet 方法。

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

您可以使用以下代码来呈现它:

-(void)showTweetSheet
{

    .
    .
    .
    //Your initialisation as before

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

}

由于这是一个 SKScene,它无法单独呈现 viewController。您需要从另一个 viewController 呈现 viewController,可以使用 self.view.window.rootViewController 属性访问该 viewController。

关于ios - 如何修复此共享按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23460592/

相关文章:

ios - 如果初始帧为 CGRectZero,则自定义 UIView 的 drawRect 永远不会被调用

ios - Apple App Store 显示应用程序的通用大小而不是应用程序的单独安装大小

IOS:如何在循环内使用 UIAlertView

html - 为什么 Twitter Feed 无法加载到我的网站中?

java - 无法解析 Twitter 中的日期

ios - WatchKit Catalog 示例代码错误

iphone - 如何从我的 iPhone 应用程序中找到整个设备的 CPU 消耗?

ios - iOS Crashlytics 报告了无数次罕见的崩溃。这种现象很好理解吗?

objective-c - 使用 ARC 按需销毁对象

facebook - 如何在 iOS 6 中使用社交框架点赞 Facebook 页面/关注 Twitter 用户?