我想在我的SpriteKit游戏中实现共享功能。我有一个按钮,我试图在Gamescene
中执行此操作。这就是我为此找到的代码:
func handleTwitter(sender: AnyObject) {
// Check if Twitter is available
if(SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter)) {
// Create the tweet
let tweet = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
tweet.setInitialText("I want to share this App: ")
tweet.addImage(UIImage(named: "shareImage"))
self.presentViewController(tweet, animated: true, completion: nil)
看起来非常符合逻辑,但我在
中遇到了问题self.presentViewController(tweet, animated: true, completion: nil)
Xcode告诉我GameScene doesn't have a member called "presentViewController"
。我是否只需要在GameViewController.swift
中实现此代码?如果是这样,我将如何在游戏场景和其他场景中使用它?我有两个带共享按钮的场景。或问题出在SLComposeViewController
?我不明白是什么问题,我是否需要在GameScene
中声明某种代表才能使其有效?
答案 0 :(得分:0)
这是适合我的代码。
UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController:tweet animated:YES completion:nil];
希望这有帮助
答案 1 :(得分:0)
那么对那些遭受这个问题的人来说。答案是:
func handleTwitter()
应该在GameViewController
。然后在viewDidLoad
中,您需要发布NSNotification:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleTwitter", name: "tweet", object: nil)
//You can use any name in the name field
//selector should be the name of a function without braces
//that you want to pass to other scenes
然后你应该去你的GameScene或GameOverScene或者其他任何东西并制作一个全局函数,它响应那个NSNotification:
func gameOvetTweet() {
NSNotificationCenter.defaultCenter().postNotificationName("tweet", object: nil)
}
//make sure that name in 'postNotificationName("THIS NAME"...'
//is matching name that you've passed in GameViewController.
//In this example it is "tweet"
然后,唯一剩下要做的就是将你的功能发布到你想要的任何地方。例如:
if self.nodeAtPoint(location) == self.twitterButton {
gameOverTweet()
}
这就是全部。我也将它用于广告。
答案 2 :(得分:0)
任何使用swift的人:
self.view?.window?.rootViewController!.presentViewController(viewController!, animated: true, completion: nil)