GameScene分享按钮

时间:2017-08-22 15:26:54

标签: swift sprite-kit share

我的游戏已准备就绪,我唯一缺少的(并且想要添加)是向社交媒体分享标准消息的选项。我环顾四周,找到了不同的方法,但是当我实现它们时,它们似乎都没有用。我有一种感觉,这可能是一件简单的事情,我做错了!

这是我的按钮:

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!

我出错的任何想法?

提前致谢!

1 个答案:

答案 0 :(得分:2)

两天前我遇到了同样的问题。我使用此stackoverflow 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)
        }
    }