NewbieWantsToMaster
NewbieWantsToMaster

Reputation: 392

How share a screenshot to Facebook using Swift?

I need to have a Facebook share button on one of my app's view controllers so that when the user pushes it, it will share a screenshot of the user's current screen to Facebook.

I have been watching a few tutorials such as this one on how to implement a Facebook share button: https://www.youtube.com/watch?v=774_-cTjnVM

But these only show how I can share a message on Facebook, and I'm still a little bit confused how to share the whole screen that user is currently interacting with.

Upvotes: 1

Views: 5481

Answers (3)

Ahmed Safadi
Ahmed Safadi

Reputation: 4600

swift 3

    let screen = UIScreen.main

    if let window = UIApplication.shared.keyWindow {
        UIGraphicsBeginImageContextWithOptions(screen.bounds.size, false, 0);
        window.drawHierarchy(in: window.bounds, afterScreenUpdates: false)
        let image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        let composeSheet = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
        composeSheet?.setInitialText("Hello, Facebook!")
        composeSheet?.add(image)

        present(composeSheet!, animated: true, completion: nil)
    }

Upvotes: 1

TwoStraws
TwoStraws

Reputation: 13127

Sharing directly to Facebook isn't hard to do. First, import the Social framework:

import Social

Now add this as the action for your button:

let screen = UIScreen.mainScreen()

if let window = UIApplication.sharedApplication().keyWindow {
    UIGraphicsBeginImageContextWithOptions(screen.bounds.size, false, 0);
    window.drawViewHierarchyInRect(window.bounds, afterScreenUpdates: false)
    let image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    let composeSheet = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
    composeSheet.setInitialText("Hello, Facebook!")
    composeSheet.addImage(image)

    presentViewController(composeSheet, animated: true, completion: nil)
}

You might be interested to know that UIActivityViewController lets users share to Facebook but also other services. The code above is for your exact question: sharing to Facebook. This code renders the entire visible screen; you can also have individual views render themselves if you want.

Note: As Duncan C points out in a comment below, this rendering code won't include anything outside your app, such as other apps or system controls.

Upvotes: 12

Duncan C
Duncan C

Reputation: 131471

In iOS 8 and earlier there used to be a private framework that you could use to capture the entire screen. Using that framework would cause your app to be rejected for the app store, but at least it worked.

Starting in iOS 9 that API no longer works

The best you can do is to capture your app's views. That won't include the status bar or other things drawn by the system or other apps.

One way is to create an off-screen context, render the parent view you want to capture into the off-screen context (probably using drawViewHierarchyInRect:afterScreenUpdates:, load the data from the context into a UIImage, and then close the context.

Another way is a new API that will capture a snapshot of a view hierarchy. One of the new methods to capture a snapshot is snapshotViewAfterScreenUpdates. That creates specialized snapshot view.

Upvotes: 4

Related Questions