我正在开发一款 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/