7

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!

tdh
  • 884
  • 1
  • 11
  • 22

2 Answers2

11

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.

PoisonedApps
  • 716
  • 8
  • 21
  • Mark my answer as correct so it has been answered and upvote please. – PoisonedApps Jan 13 '15 at 23:44
  • How would I go about implementing the screenshot as the image? I used your method, and it worked, until I tried to implement the screen shot code. The screenshot would come up blank, and Messages still wouldn't be an option. Thanks! ; I do appreciate your answer, but I'm afraid I don't have enough reputation to upvote it. I will mark it correct, and I'll make sure to build up the reputation to upvote it. – tdh Jan 14 '15 at 01:09
  • Thank you. I'll have a look into the screenshot. Also are you using the simulator to test your app? – PoisonedApps Jan 14 '15 at 02:08
  • I am using the simulator to test the app, although the screenshot is coming up blank. Thank you for answering so quickly! – tdh Jan 14 '15 at 05:13
  • The simulator doesn't have a messaging app so that won't appear in the controller but it will on a real device. As for the screenshot you need to look at other questions or start a new questions about why you are generating blank screenshots. For testing try saving it to camera roll first. Once you have that working then you can try sharing it again. – PoisonedApps Jan 14 '15 at 20:00
  • Thank you so much! You have been such great help, and I'll definitely build up the reputation to upvote your answer. – tdh Jan 15 '15 at 05:52
  • But build error:`ViewController.swift:181:22: Expected parameter type following ':'` at the line `func socialShare(#sharingText: String?, sharingImage: UIImage?, sharingURL: NSURL?) {` – Gank Jul 30 '16 at 14:45
0

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/

Jon Winstanley
  • 23,010
  • 22
  • 73
  • 116
ericgu
  • 2,229
  • 23
  • 25
  • `ViewController.swift:207:36: Value of optional type 'CGContext?' not unwrapped; did you mean to use '!' or '?'?` But build error at the line `view.layer.renderInContext(UIGraphicsGetCurrentContext())` – Gank Jul 30 '16 at 15:58