GameScene 分享按钮
GameScene Share Button
我的游戏已经基本准备就绪,我唯一缺少(并想添加)的是向社交媒体分享标准消息的选项。我环顾四周,发现了不同的方法来执行此操作,但是 none 在我实施它们时它们似乎有效。我有一种感觉,我做错了可能很简单!
这是我的按钮:
func createShareButton() {
shareButton = SKSpriteNode(imageNamed: "shareBtn")
shareButton.size = CGSize(width: 240, height: 55)
shareButton.position = CGPoint(x: self.frame.width / 2, y: (self.frame.height / 2) - 150)
shareButton.zPosition = 6
self.addChild(shareButton)
shareButton.run(SKAction.scale(to: 1.0, duration: 0.3))
}
这是按下按钮时的代码(在我的 GameScene.swift 文件中):
let vc = self.view?.window?.rootViewController
let myText = "share test message!"
let activityVC:UIActivityViewController = UIActivityViewController(activityItems: [myText], applicationActivities: nil)
vc?.present(activityVC, animated: true, completion: nil)
当我点击按钮时,控制台出现错误:
Warning: Attempt to present <UIActivityViewController: 0x149d4e880> on <MyGame.initialVC: 0x149d0fe90> whose view is not in the window hierarchy!
我哪里出错了?
提前致谢!
我 运行 两天前也遇到过同样的问题。我用 Whosebug answer
修复了它
我一直收到同样的错误,这是因为 rootViewController 不是层次结构中的当前顶部 viewController。当我想要 viewController 控制当前 SKScene 时,它试图使用我的初始 viewController。
希望对您有所帮助
func shareGame() {
if var top = scene?.view?.window?.rootViewController {
while let presentedViewController = top.presentedViewController {
top = presentedViewController
}
let activityVC = UIActivityViewController(activityItems: ["I am playing Crashbox! Check it out."], applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = view
top.present(activityVC, animated: true, completion: nil)
}
}
我的游戏已经基本准备就绪,我唯一缺少(并想添加)的是向社交媒体分享标准消息的选项。我环顾四周,发现了不同的方法来执行此操作,但是 none 在我实施它们时它们似乎有效。我有一种感觉,我做错了可能很简单!
这是我的按钮:
func createShareButton() {
shareButton = SKSpriteNode(imageNamed: "shareBtn")
shareButton.size = CGSize(width: 240, height: 55)
shareButton.position = CGPoint(x: self.frame.width / 2, y: (self.frame.height / 2) - 150)
shareButton.zPosition = 6
self.addChild(shareButton)
shareButton.run(SKAction.scale(to: 1.0, duration: 0.3))
}
这是按下按钮时的代码(在我的 GameScene.swift 文件中):
let vc = self.view?.window?.rootViewController
let myText = "share test message!"
let activityVC:UIActivityViewController = UIActivityViewController(activityItems: [myText], applicationActivities: nil)
vc?.present(activityVC, animated: true, completion: nil)
当我点击按钮时,控制台出现错误:
Warning: Attempt to present <UIActivityViewController: 0x149d4e880> on <MyGame.initialVC: 0x149d0fe90> whose view is not in the window hierarchy!
我哪里出错了?
提前致谢!
我 运行 两天前也遇到过同样的问题。我用 Whosebug answer
修复了它我一直收到同样的错误,这是因为 rootViewController 不是层次结构中的当前顶部 viewController。当我想要 viewController 控制当前 SKScene 时,它试图使用我的初始 viewController。
希望对您有所帮助
func shareGame() {
if var top = scene?.view?.window?.rootViewController {
while let presentedViewController = top.presentedViewController {
top = presentedViewController
}
let activityVC = UIActivityViewController(activityItems: ["I am playing Crashbox! Check it out."], applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = view
top.present(activityVC, animated: true, completion: nil)
}
}