Swift SKScene 中的社交框架

Social Framework in SKScene in Swift

这个问题已经在 Stack Overflow 上被问了很多次了,但仍然没有确定的答案。这就是我再次提问的原因。希望以一种清晰的方式。

如何从 SKScene 调用 viewController 的社交功能?

这是我的 SKScene 中的 touchesBegan 函数:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)

        if self.nodeAtPoint(location) == self.twitterButton {
            println("Twitter button")
        }

        if self.nodeAtPoint(location) == self.facebookButton {
            println("Facebook button")
        }
    }
}

这是我的 GameViewController 中的函数:

func postToFacebook() {

    if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
        var controller = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
        controller.setInitialText("Testing Posting to Facebook")
        self.presentViewController(controller, animated:true, completion:nil)

    } else {
        println("no Facebook account found on device")
    }
}

您使用 NSNotificationCenter 显示 SKScene 中的 viewController。请记住添加观察者,然后 post 通知。不要忘记在您不再需要 NSNotification 时取消初始化它,否则您的应用程序将崩溃。

来自 here 的示例代码。只需将代码的名称从 twitter 调整为 facebook,它应该可以工作。

func showTweetSheet() {
let tweetSheet = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
tweetSheet.completionHandler = {
    result in
    switch result {
    case SLComposeViewControllerResult.Cancelled:
        //Add code to deal with it being cancelled
        break

    case SLComposeViewControllerResult.Done:
        //Add code here to deal with it being completed
        //Remember that dimissing the view is done for you, and sending the tweet to social media is automatic too. You could use this to give in game rewards?
        break
    }
}

tweetSheet.setInitialText("Test Twitter") //The default text in the tweet 
tweetSheet.addImage(UIImage(named: "TestImage.png")) //Add an image if you like?
tweetSheet.addURL(NSURL(string: "http://twitter.com")) //A url which takes you into safari if tapped on

self.presentViewController(tweetSheet, animated: false, completion: {
    //Optional completion statement
    })

}