riverhawk
riverhawk

Reputation: 236

How do you present a view controller in a SKScene?

I am trying to figure out how to use a share sheet for my SpriteKit game and when I try to present the view controller from my scene, it gives me an error.

if self.nodeAtPoint(location) == self.shareButton {
        var tempMessage = "I just got \(score) on [insert app]"

        let activityVC:UIActivityViewController = UIActivityViewController(activityItems: [tempMessage], applicationActivities: nil)

        self.presentViewController(activityVC, animated: true, completion: nil)
}

This is in my GameOverScene. How exactly do you go about managing viewcontrollers in Swift because the answers I have seen were in OBJ-C.

Thanks!

Upvotes: 0

Views: 821

Answers (1)

riverhawk
riverhawk

Reputation: 236

Ok so I did some digging and found a solution that works.

Basically, I used NSNoticationCenter and set up the view controller in my GameViewController file as such.

override func viewDidLoad() {
.
.
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showSocialNetworks", name: "showSocialNetworksID", object: nil)
}

and made the function to be called

func showSocialNetworks(){
    var tempMessage = "Insert your message to be shared on social network"

    let activityVC:UIActivityViewController = UIActivityViewController(activityItems: [tempMessage], applicationActivities: nil)

    self.presentViewController(activityVC, animated: true, completion: nil)
}

Now jump back to my GameOverScene, I set it up so when the user taps the Share Button, it performs the following code

if self.nodeAtPoint(location) == self.shareButton {
             NSNotificationCenter.defaultCenter().postNotificationName("showSocialNetworksID",  object: nil)

}

This may not be the cleanest way to do it, but it works for me. Hope this helps!

Upvotes: 3

Related Questions