0

my game is pretty much ready to go, the only thing I am missing (and would like to add) is the option to share a standard message to social media. I have looked around and found different ways of doing this but none of them seem to work when I implement them. I've got a feeling it might be something simple that I'm doing wrong!

Here's my button:

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))
}

And here's my code when the button is pressed (inside my GameScene.swift file):

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)

When I tap the button I get an error in the console:

Warning: Attempt to present <UIActivityViewController: 0x149d4e880> on <MyGame.initialVC: 0x149d0fe90> whose view is not in the window hierarchy!

Any ideas where I am going wrong?

Thanks in advance!

evorg88
  • 215
  • 2
  • 12
  • Yes, the root view controller's view is not on the window hierarchy, like the warning is telling you. This means you probably have another view from a different view controller showing. – Knight0fDragon Aug 22 '17 at 15:44
  • Ok so basically when the user opens the app, they are presented with a view that I have designed on the storyboard called InitialVC. When they press the Start button, they are sent to GameViewController and shown the GameScene. Is this what is causing the issue? The fact I am showing my InitalVC first? How would I get around this? – evorg88 Aug 22 '17 at 16:25
  • You need to get whatever view controller is on screen. If you are using a storyboard, then your activityviewcontroller should also be on the storyboard, and you should just make a segue to it – Knight0fDragon Aug 22 '17 at 21:03

1 Answers1

3

I ran into this same issue two days ago. I fixed it using this stackoverflow answer

I kept getting the same error and it was because rootViewController was not the current top viewController in the hierarchy. It was trying to use my initial viewController when I wanted the viewController in control of the current SKScene.

Hope this helps

    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)
        }
    }
ivan1ne
  • 46
  • 4