-5

I have come across a few posts in different places, but do not think they fully cover what I am looking for.

By tapping a bar button item called 'Share', I would like my app to take a screenshot of the current view in the app and pop the sharing window up to share it with Twitter, Facebook and the rest of them.

Laroms
  • 85
  • 1
  • 13

1 Answers1

0

You can add share button

var shareButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Action, target: self, action: "shareButtonPressed:")
self.navigationItem.rightBarButtonItem = shareButton

and populate share options

func shareButtonPressed(sender: AnyObject) {
    NSLog("shareButton pressed")
    let stringtoshare: String = "This is a string to share"

    //add current screen shot image to share
    let imagetoshare = captureScreen()

    let activityItems: [AnyObject] = [stringtoshare, imagetoshare]
    let activityVC: UIActivityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
    activityVC.excludedActivityTypes = [UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypePostToFacebook, UIActivityTypePostToTwitter, UIActivityTypePostToWeibo]
    self.presentViewController(activityVC, animated: TRUE, completion: { _ in })
}

//Capture screen shot image

func captureScreen() -> UIImage
{

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0);

    self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)

    var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image
}

Please let me know if you find any trouble in this, I am not able to test this code snip (as I am not at system) but this is how i used to handle in apps.

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
  • I have not tried it yet, but I wish to drag that bar button item from the object library and connect it with @IBAction - by doing like you suggested, the button will hide my other button items. How would you implement with @IBAction? – Laroms Apr 12 '16 at 19:06
  • Thanks for the edited answer - can you show how you do it with a bar button that you drag from the object library? – Laroms Apr 13 '16 at 08:01
  • Ok - I tried the above code but I am getting an error with **let imagetoshare: UIImage = img**. The message is _use of unresolved identifier 'img'_. Any thoughts? – Laroms Apr 13 '16 at 08:13
  • @Laroms because you need to add any dummy image in your project, which you want to share with taxt, I have updated the code, just add any image named "yourImage.png" and test – swiftBoy Apr 13 '16 at 08:17
  • Ah ok! But what if the image I want to share is what is currently displayed on the iPhone screen? – Laroms Apr 13 '16 at 08:26
  • @Laroms Added the logic for capturing screen shot image and sending for share – swiftBoy Apr 13 '16 at 08:32
  • I am giving it a shot! – Laroms Apr 13 '16 at 08:34
  • Nice!! It works ;). Answer accepted!! Can I ask you how you would do the same but with @IBAction for button item dragged onto the nav bar? – Laroms Apr 13 '16 at 11:42
  • I am glad, it helps you, you need to create [IBOutlet for UIBarButtonItem in a UIViewController](http://stackoverflow.com/questions/19199382/cant-create-iboutlet-for-uibarbuttonitem-in-a-uiviewcontroller) then [add IBAction method](http://stackoverflow.com/questions/17979385/adding-action-and-target-to-a-custom-navigation-barbuttonitem) – swiftBoy Apr 13 '16 at 12:11