0

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!

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
riverhawk
  • 236
  • 2
  • 12
  • What error are you getting? – Wayne Tanner Feb 07 '15 at 04:40
  • "GameOverScene does not have a member named presentViewController" – riverhawk Feb 07 '15 at 04:44
  • 1
    the `self.presentViewController` has to be called from the ViewController containing the view of the `SKScene`. – rakeshbs Feb 07 '15 at 04:57
  • possible duplicate of [Present another View Controller from SkScene](http://stackoverflow.com/questions/23756865/present-another-view-controller-from-skscene) and from the looks of it, several others too: http://stackoverflow.com/search?q=view+controller+skscene+present – CodeSmile Feb 07 '15 at 10:18

1 Answers1

3

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!

riverhawk
  • 236
  • 2
  • 12