Devin Reid
Devin Reid

Reputation: 13

How to implement Facebook and Twitter posting in my Sprite Kit game app?

I have tried everything that I have seen on Stack Overflow and everywhere else online, but I cannot figure out how to present a viewController from my SKScene class. I have 3 scenes and in one of them I have Facebook/Twitter nodes named "buttonFacebook" and "buttonTwitter". I am using a Switch statement to check the node name in touchesBegan.

The best post I can find that I think has me the closest to accomplishing this is here: swift spritekit Facebook share button

Inside of my SKScene class "GameSceneGamePlay" I have this Case inside if my Switch to recognize when the Facebook button is touched:

case "facebook":
        func showTweetSheet() {
            let tweetSheet = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
            tweetSheet.completionHandler = {
                result in
                switch result {
                case SLComposeViewControllerResult.Cancelled:
                    //Add code to deal with it being cancelled
                    break

                case SLComposeViewControllerResult.Done:
                    //Add code here to deal with it being completed
                    //Remember that dimissing the view is done for you, and sending the tweet to social media is automatic too. You could use this to give in game rewards?
                    break
                }
            }

            tweetSheet.setInitialText("Test Twitter") //The default text in the tweet
            tweetSheet.addImage(UIImage(named: "TestImage.png")) //Add an image if you like?
            tweetSheet.addURL(NSURL(string: "http://twitter.com")) //A url which takes you into safari if tapped on

            self.presentViewController(tweetSheet, animated: false, completion: {
                //Optional completion statement
            })
        }

I get an error when I try to execute self.presentViewController(tweetSheet, animated: false, .... "GameSceneGamePlay does not have a member named presentViewController".

I am trying to use these 2 statements from the post I referenced above to trigger the function, but I cannot get past the error for presentViewController.

NSNotificationCenter.defaultCenter().postNotificationName("facebookPost", object: nil)

NSNotificationCenter.defaultCenter().addObserver(self, selector: "showTweetSheet", name: "facebookPost", object: nil)

Can someone give me some advice how to get past this error, or if/how I need to create a new viewController? I have tried creating a class with UIViewController but can't get that to present either.

Upvotes: 0

Views: 630

Answers (1)

pbodsk
pbodsk

Reputation: 6876

Your problem is that you are trying to present a UIViewController from your GameSceneGamePlay, but as you write yourself, GameSceneGamePlay is an instance of SKScene, not a UIViewController.

presentViewController is a method found on UIViewController, not SKScene, which is why you get an error.

To get around this, you could keep a reference to the UIViewController that presents your SKScene, as described in this post and then ask that view controller to present your tweetSheet.

Upvotes: 1

Related Questions