根据你的问题,我假设你正在寻找一些社交媒体发布。
您可以将视图控制器的引用传递给 SKScene,也可以使用NSNotificationCenter
反而。我更喜欢使用后者。
首先确保您已将 Social.framework 添加到您的项目中。
将社交框架导入到您的视图控制器中#import <Social/Social.h>
然后在你的视图控制器中viewDidLoad
方法添加此代码:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(createTweet:)
name:@"CreateTweet"
object:nil];
接下来将此方法添加到您的视图控制器中:
-(void)createTweet:(NSNotification *)notification
{
NSDictionary *tweetData = [notification userInfo];
NSString *tweetText = (NSString *)[tweetData objectForKey:@"tweetText"];
NSLog(@"%@",tweetText);
// build your tweet, facebook, etc...
SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
在 SKScene 中的适当位置(赢得比赛、输掉比赛等)添加以下代码:
NSString *tweetText = @"I just beat the last level.";
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:tweetText forKey:@"tweetText"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"CreateTweet" object:self userInfo:userInfo];
上面的代码发送一个带有文本的 NSNotification,您的视图控制器将拾取该文本并执行指定的方法(在上面的示例中是 createTweet)。