Ios 如何在swift中锁定Facebook共享按钮中的文本?
我正在尝试在我的spritekit swift游戏中实现共享按钮。它工作得很好,但我不知道如何锁定文本,所以用户在发布到facebook之前不能更改它。谢谢你的帮助 我的代码如下所示:Ios 如何在swift中锁定Facebook共享按钮中的文本?,ios,facebook,swift,button,share,Ios,Facebook,Swift,Button,Share,我正在尝试在我的spritekit swift游戏中实现共享按钮。它工作得很好,但我不知道如何锁定文本,所以用户在发布到facebook之前不能更改它。谢谢你的帮助 我的代码如下所示: func shareToFacebook() { var sharetoface : SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook) sharetoface.setI
func shareToFacebook() {
var sharetoface : SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
sharetoface.setInitialText("My score was \(score)")
let vc: UIViewController = self.view!.window!.rootViewController!
vc.presentViewController(sharetoface, animated: true, completion: nil)
}
SLComposeViewController
是UIViewController
的子类,因此它有一个与之关联的视图。因此,您可以首先通过调用SLComposeViewController
的属性view
来获取其视图,并使用view.subview
搜索该视图的子视图,然后查找TextField。然后可以将UITextField实例的enabled属性设置为false
func shareToFacebook() {
var sharetoface : SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
sharetoface.setInitialText("My score was \(score)")
/* New code added */
let view = sharetofave.view as! UIView // as! Forced unwrapped as of Swift 1.2 ß3
let subviews = view.subviews
let textField: UITextField // no need to initialize let constants as soon as possible as of Swift 1.2 ß3
for v in subviews {
if v is UITextField {
textField = v as! UITextField
}
}
textField.enabled = false
/* New code added */
let vc: UIViewController = self.view!.window!.rootViewController!
vc.presentViewController(sharetoface, animated: true, completion: nil)
}
// Using SLComposeServiceViewController
func newShareToFacebook() {
var sharetoface : SLComposeServiceViewController = SLComposeServiceViewController()
sharetoface.textView.text = "My score was \(score)"
sharetoface.textView.enabled = false
let vc: UIViewController = self.view!.window!.rootViewController!
vc.presentViewController(sharetoace, animated: true, completion: nil)
}
“因此用户在发布到facebook之前不能更改它”-您不允许以任何方式预填充帖子的消息部分,它必须是100%由用户生成的,即由用户键入。你应该去阅读平台政策。我知道这一点,但我希望用户发布他们的高分,如果他们能够发布他们想要的任何东西,这就有点失去了意义……好吧,这仍然不能证明违反政策是正当的。你根本不允许将他们的高分作为帖子的信息部分发布。使用游戏的分数API,或者发布在标题/描述中包含该数据的开放图形对象。感谢您的回复,我尝试实现它,但子视图变量为空,因此我无法在其中搜索文本字段…@tommhol try newShareToFacebook()方法,我已将该方法添加到以前的答案中。它使用SLComposeServiceViewController而不是SLComposeViewController