3

I want there to be a button in my app, that when it is pressed, this(See image below) Pops up. How do i do that? I don't want to create a custom sharing extion, i just want the default one? What code do i use? All the tutorials online are in objective-c. Please give an answer in swift.

Image: http://9to5mac.com/2014/06/30/hands-on-1password-beta-shows-off-ios-8s-touch-id-extensions-apis-video/#jp-carousel-330420

Here is my code so far, but i get an error that UIBarButtonItem Is not convetable to UIVIew Why? The action is connected to a navigation bar button item?

@IBAction func ActionButton(sender: UIBarButtonItem) {

    let firstActivityItem = "Text you want"
    let secondActivityItem : NSURL = NSURL(string: "http//:urlyouwant")!
    // If you want to put an image
    let image : UIImage = UIImage(named: "TALogo")!

    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! UIBarButtonItem)

    // This line remove the arrow of the popover to show in iPad
    activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.allZeros
    activityViewController.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0)

    // Anything you want to exclude
    activityViewController.excludedActivityTypes = [
        UIActivityTypePostToWeibo,
        UIActivityTypePrint,
        UIActivityTypeAssignToContact,
        UIActivityTypeSaveToCameraRoll,
        UIActivityTypeAddToReadingList,
        UIActivityTypePostToFlickr,
        UIActivityTypePostToVimeo,
        UIActivityTypePostToTencentWeibo
    ]

    self.presentViewController(activityViewController, animated: true, completion: nil)



}
nachshon fertel
  • 121
  • 1
  • 6

1 Answers1

6

The way is using UIActivityViewController for example in the following way :

@IBAction func shareSheet(sender: AnyObject) {

    let firstActivityItem = "Text you want"
    let secondActivityItem : NSURL = NSURL(string: "http//:urlyouwant")!
    // If you want to put an image
    let image : UIImage = UIImage(named: "image.jpg")!

    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.allZeros
    activityViewController.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0)

    // Anything you want to exclude
    activityViewController.excludedActivityTypes = [
        UIActivityTypePostToWeibo,
        UIActivityTypePrint,
        UIActivityTypeAssignToContact,
        UIActivityTypeSaveToCameraRoll,
        UIActivityTypeAddToReadingList,
        UIActivityTypePostToFlickr,
        UIActivityTypePostToVimeo,
        UIActivityTypePostToTencentWeibo
    ]

    self.presentViewController(activityViewController, animated: true, completion: nil)
}

The above code works for both iPhone and iPad because in you set the new popoverPresentationController in iOS 8 it works for iPad too.

In the case of use an UIBarButtonItem you need to replace this line:

activityViewController.popoverPresentationController?.sourceView = (sender as! UIButton)

With this one:

activityViewController.popoverPresentationController?.barButtonItem = (sender as! UIBarButtonItem)

I hope this help you.

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
  • Not please read my answer again, I put the comments for iPad, it works for boths – Victor Sigler May 07 '15 at 20:47
  • Ok, I see. How do i add more features to it, like facebook, and twitter? – nachshon fertel May 07 '15 at 20:48
  • The app crashed here, when useing an ipad. Why? – nachshon fertel May 07 '15 at 20:50
  • activityViewController.popoverPresentationController?.sourceView = (sender as! UIButton) – nachshon fertel May 07 '15 at 20:50
  • Facebook and Twitter work fine with the code I put above and share a text, a url and a image. But be careful with Facebook because it have some troubles if the app is downloaded, etc. It only happens with Facebooks because changes in his policy!! – Victor Sigler May 07 '15 at 20:50
  • The line `activityViewController.popoverPresentationController?.sourceView = (sender as! UIButton)` is only to cast the sender as `UIButton` because in the `@IBAction` I set the `sender` as `AnyObject`, See updated answer, you can change it as I put above too. I don't know why is your error – Victor Sigler May 07 '15 at 20:55
  • Does the app crash there because the button is a UIBarButtonItem – nachshon fertel May 07 '15 at 20:55
  • You have to define the `sender` as you want, I only put an example using an `UIButton` to see the share , if you want to use it with a `UIBarButtonItem` then you need to cast this line in the following way: `activityViewController.popoverPresentationController?.sourceView = (sender as! UIBarButtomItem)` and let the parameter in the action as `AnyObject` – Victor Sigler May 07 '15 at 20:57
  • As i put in my answer you need to cast the sender for the type of the object you set the `@IBAction` nothing more – Victor Sigler May 07 '15 at 21:01
  • I did it, and i got an error saying: "UIBarButtonItem Is not convertible to UIVIew" See updated question – nachshon fertel May 07 '15 at 21:04
  • The `popoverPresentationController` have a property named `barButtonItem` in the case of the view has a `UIBarButtonItem` – Victor Sigler May 07 '15 at 21:13
  • For the record @nachshonfertel You need to be more concise in the way you ask and explain all your problem, for example say that what you want is for iPad and you have a `UIBarButtonItem` and in this way we gain time – Victor Sigler May 07 '15 at 21:15