Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 478
tdh Avatar asked Jan 13 '15 04:01

tdh


1 Answers

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.

like image 144
PoisonedApps Avatar answered Oct 10 '22 21:10

PoisonedApps