0
votes

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!

1
What error are you getting?Wayne Tanner
"GameOverScene does not have a member named presentViewController"riverhawk
the self.presentViewController has to be called from the ViewController containing the view of the SKScene.rakeshbs
possible duplicate of Present another View Controller from SkScene and from the looks of it, several others too: stackoverflow.com/search?q=view+controller+skscene+presentLearnCocos2D

1 Answers

3
votes

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!