tdh
tdh

Reputation: 872

Sharing A Screenshot in Activity View Controller - Swift

I'm developing part of an app so that when you tap the share button, it allows you to instantly share a screenshot of your highscore along with a message. I haven't been able to produce/share a screenshot, and when I tap the share button, the app only allows me to copy my default text or "Mail" my default text, not allowing me to post to Facebook, Twitter, Messages, and more.

func shareButtonPress() {

    var postPhrase = "Just hit \(highscore)! Beat it! #SwypIt"

    //Generate the screenshot
    UIGraphicsBeginImageContext(view.frame.size)
    view.layer.renderInContext(UIGraphicsGetCurrentContext())
    var image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    var postImage = UIImage(named: "\(image)")

    var activityViewController : UIActivityViewController = UIActivityViewController(activityItems: [postPhrase, postImage!], applicationActivities: nil)

    self.presentViewController(activityViewController, animated: true, completion: nil)

}

What is the best way of going about this? Thanks!

Upvotes: 7

Views: 5475

Answers (2)

ericgu
ericgu

Reputation: 2249

Use a SLComposeViewController.

 import Social

 func shareButtonPress() {

   var postPhrase = "New high score \(highscore)!"

   //Generate the screenshot
   UIGraphicsBeginImageContext(view.frame.size)
   view.layer.renderInContext(UIGraphicsGetCurrentContext())
   var image = UIGraphicsGetImageFromCurrentImageContext()
   UIGraphicsEndImageContext()

   var postImage = UIImage(named: "\(image)")

   let shareToFacebook = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
   shareToFacebook.setInitialText(postPhrase)
   shareToFacebook.addImage(postImage)
   presentViewController(shareToFacebook, animated: true, completion: nil)
 }

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Reference/SLComposeViewController_Class/

Upvotes: 0

PoisonedApps
PoisonedApps

Reputation: 726

This is how i handle sharing in my app.

    func socialShare(#sharingText: String?, sharingImage: UIImage?, sharingURL: NSURL?) {
    var sharingItems = [AnyObject]()

    if let text = sharingText {
        sharingItems.append(text)
    }
    if let image = sharingImage {
        sharingItems.append(image)
    }
    if let url = sharingURL {
        sharingItems.append(url)
    }

    let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
    activityViewController.excludedActivityTypes = [UIActivityTypeCopyToPasteboard,UIActivityTypeAirDrop,UIActivityTypeAddToReadingList,UIActivityTypeAssignToContact,UIActivityTypePostToTencentWeibo,UIActivityTypePostToVimeo,UIActivityTypePrint,UIActivityTypeSaveToCameraRoll,UIActivityTypePostToWeibo]
    self.presentViewController(activityViewController, animated: true, completion: nil)
}

I have excluded a number of sharing options using .excludedActvityTypes.

Then whenever you hit the share button have it call this

socialShare(sharingText: "Just hit \(highscore)! Beat it! #SwypI", sharingImage: UIImage(named: "The screenshot you are saving"), sharingURL: NSURL(string: "http://itunes.apple.com/app/"))

The reason you are not seeing Twitter and Facebook as sharing options is because you need to be signed into them within the settings on the IPhone. Not the individual apps.

Hope this helps.

Upvotes: 11

Related Questions