Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement "share button" in Swift

This is the some peace of code for twitter... I want to know how to get the share action view like we get in iOS stock photos app...

@IBAction func twitterButton(sender: AnyObject) {              let image: UIImage = UIImage(named: "LaunchScreenImage.png")!                  let twitterControl = SLComposeViewController(forServiceType: SLServiceTypeTwitter)         twitterControl.setInitialText("")         twitterControl.addImage(image)                  let completionHandler = {(result:SLComposeViewControllerResult) -> () in             twitterControl.dismissViewControllerAnimated(true, completion: nil)             switch(result){             case SLComposeViewControllerResult.Cancelled:                 print("User canceled", terminator: "")             case SLComposeViewControllerResult.Done:                 print("User tweeted", terminator: "")             }     }         twitterControl.completionHandler = completionHandler         self.presentViewController(twitterControl, animated: true, completion: nil)  } 
like image 836
Santosh Avatar asked Jun 21 '16 08:06

Santosh


People also ask

How do I share in swift?

In several iOS apps, such as Safari or the Camera app, you can click a button that brings up an interface that makes it easy to send or share what you are looking at via messages, Twitter, Facebook, etc. That interface is known as the UIActivityViewController.

What is the share button on Facebook?

When someone hits the Facebook Share button, they can publish a post that they're interested in on their own wall, without having to copy and paste a link onto their Facebook profile. The Share button is one of the three engagement options that Facebook gives users to allow them to connect with people online.

What is UIActivityViewController in swift?

A view controller that you use to offer standard services from your app.

How do I resize an image in swift 5?

Image resize function in swift as below. func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage { let size = image. size let widthRatio = targetSize. width / size.


Video Answer


1 Answers

Swift 5:

    // Setting description     let firstActivityItem = "Description you want.."      // Setting url     let secondActivityItem : NSURL = NSURL(string: "http://your-url.com/")!          // If you want to use an image     let image : UIImage = UIImage(named: "your-image-name")!     let activityViewController : UIActivityViewController = UIActivityViewController(         activityItems: [firstActivityItem, secondActivityItem, image], applicationActivities: nil)          // This lines is for the popover you need to show in iPad     activityViewController.popoverPresentationController?.sourceView = (sender as! UIButton)          // This line remove the arrow of the popover to show in iPad     activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.down     activityViewController.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0)          // Pre-configuring activity items     activityViewController.activityItemsConfiguration = [     UIActivity.ActivityType.message     ] as? UIActivityItemsConfigurationReading          // Anything you want to exclude     activityViewController.excludedActivityTypes = [         UIActivity.ActivityType.postToWeibo,         UIActivity.ActivityType.print,         UIActivity.ActivityType.assignToContact,         UIActivity.ActivityType.saveToCameraRoll,         UIActivity.ActivityType.addToReadingList,         UIActivity.ActivityType.postToFlickr,         UIActivity.ActivityType.postToVimeo,         UIActivity.ActivityType.postToTencentWeibo,         UIActivity.ActivityType.postToFacebook     ]          activityViewController.isModalInPresentation = true     self.present(activityViewController, animated: true, completion: nil) 
like image 177
Santosh Avatar answered Sep 25 '22 09:09

Santosh