gpt4 book ai didi

ios - Swift 中 SKScene 中的社交框架

转载 作者:搜寻专家 更新时间:2023-11-01 06:46:19 25 4
gpt4 key购买 nike

这个问题已经在 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")
}
}

最佳答案

您使用 NSNotificationCenterSKScene 中呈现 viewController。记得添加观察者,然后发布通知。不要忘记在您不再需要 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com