Sharing a screenshot on iOS using Swift and Sprite Kit.

Written by Michael on February 13, 2015

One way I have found that works well for marketing a game is to give players the ability to share a screenshot of their score on Twitter or Facebook. It works well because as soon as people start sharing their score, their friends get curious and want to play too. In this post I will run through how I implemented the ability to share a screenshot to Twitter or Facebook using Swift and Sprite Kit.

The following class takes care of sharing the player's score to Facebook or Twitter:

import Foundation
import UIKit
import SpriteKit

class Social {

  func shareScore(scene: SKScene) {
    let postText: String = "Check out my score! Can you beat it?"
    let postImage: UIImage = getScreenshot(scene)
    let activityItems = [postText, postImage]
    let activityController = UIActivityViewController(
      activityItems: activityItems,
      applicationActivities: nil
    )

    var controller: UIViewController = scene.view!.window!.rootViewController!

    controller.presentViewController(
      activityController,
      animated: true,
      completion: nil
    )
  }

  func getScreenshot(scene: SKScene) -> UIImage {
    let snapshotView = scene.view!.snapshotViewAfterScreenUpdates(true)
    let bounds = UIScreen.mainScreen().bounds

    UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0)

    snapshotView.drawViewHierarchyInRect(bounds, afterScreenUpdates: true)

    var screenshotImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    return screenshotImage;
  }
}

When shareScore() is called the following comes up on the iPhone:

Sharing image using Sprite Kit and Swift

Then when you click to share on twitter you get the following screen:

Sharing image on Twitter using Sprite Kit and Swift

You get a similar screen for sharing on Facebook. You can use this marketing technique to drive more people to your game, then monetize by displaying Ads or having in game purchases.

comments powered by Disqus