Sharing a screenshot in an activity controller - Swift

I am developing a part of the application, so that when you press the sharing button, you can instantly share a screenshot with your record along with the message. I was not able to create / publish a screenshot, and when I click the Share button, the application allows me to copy the default text or Mail by default, not allowing me to post to Facebook, Twitter, messages, and much 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 to get around this? Thanks!

+7
swift screenshot uiactivityviewcontroller share
source share
2 answers

This is how I handle sharing in my application.

  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) } , UIActivityTypeAssignToContact, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePrint, UIActivityTypeSaveToCameraRoll, UIActivityTypePostToWeibo]  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 ruled out several sharing options using .excludedActvityTypes.

Then when you click the sharing button, call it

 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 don’t see Twitter and Facebook as sharing options is because you need to enter them in the settings on the iPhone. Not separate applications.

Hope this helps.

+10
source share

Use 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/

0
source share

All Articles