在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
来显示viewController
中的SKScene
。请记住添加观察者,然后发布通知。当您不再需要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
})
}
关于ios - Swift中SKScene中的社交框架,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30358966/