1

This question has been asked an awful amount of times on Stack Overflow, but still without a conclusive answer. This is why I'm asking again. Hopefully in a clear way.

How do I call the social function from the viewController from an SKScene?

This is the touchesBegan function in my SKScene:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)

        if self.nodeAtPoint(location) == self.twitterButton {
            println("Twitter button")
        }

        if self.nodeAtPoint(location) == self.facebookButton {
            println("Facebook button")
        }
    }
}

This is the function inside of my GameViewController:

func postToFacebook() {

    if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
        var controller = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
        controller.setInitialText("Testing Posting to Facebook")
        self.presentViewController(controller, animated:true, completion:nil)

    } else {
        println("no Facebook account found on device")
    }
}
Nick Groeneveld
  • 895
  • 6
  • 18
  • There are a bunch of tutorials on this subject! http://www.ioscreator.com/tutorials/facebook-tutorial-ios8-swift and http://sagarrkothari.com/sharing-via-facebook-in-ios-in-swift-using-slcomposeviewcontroller/ and http://www.thedarkdev.com/social-framework-ios8-using-swift-language.html – sangony May 20 '15 at 23:26
  • @sangony yes, but they are not for spritekit! – Nick Groeneveld May 21 '15 at 05:23
  • Again, use Google before asking! A simple search with "SLComposeViewController sprite kit swift" brings up this http://stackoverflow.com/questions/25595601/swift-spritekit-facebook-share-button – sangony May 21 '15 at 12:44

1 Answers1

3

You make use of NSNotificationCenter to present the viewController from an SKScene. Remember to add the observer and then post the notification. Don't forget to deinit the NSNotification when you no longer need it or your app will crash.

An example code from here. Just tweak the code's name from twitter to facebook and it should work.

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

}

Community
  • 1
  • 1
Wraithseeker
  • 1,884
  • 2
  • 19
  • 34