1

I am building an app with a UIAlertController, and I want to have a button that says "Share" and then opens up something like

this

it seems easy to do, if it wasn't a sub-button.

My Code:

}


@IBAction func buttonTapped(sender: UIButton) {

    var title: String

    if sender.tag == correctAnswer {
        title = "Correct"
        ++score
    } else {
        title = "Wrong"

        let ac = UIAlertController(title: title, message: "Your score was \(score).", preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "Try Again", style: .Default, handler: askQuestion))
        ac.addAction(UIAlertAction(title: "Share", style: .Default, handler: share))
        presentViewController(ac, animated: true, completion: nil)
        gameOver = true
        score = 0

    }

My share function is currently not defined.

Hugo Smith
  • 11
  • 3

3 Answers3

2

You can define in your completion handler to open an UIActivityController after the UIAlertAction is tapped, nevertheless by default Apple in iPhone not show the share as you want, in iPad yes. With the following code you can show the share after the button is tapped:

let message = "The message"
let alertController = UIAlertController(title: "The title", message: message, preferredStyle: .Alert)

let shareAction = UIAlertAction(title: "Share", style: .Default, handler: { x -> Void in

     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)

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

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

alertController.addAction(shareAction)
self.presentViewController(alertController, animated: true, completion: nil)

In the above code I summarize the add more buttons, and I assume that you want the code for iPhone, if you want it too for iPad you can check my answers in this questions:

I hope this help you.

Community
  • 1
  • 1
Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
0

Do something like this, if your share method doesn't work

ac.addAction(UIAlertAction(title: "Share", style: .Default, handler: { (action) -> Void in 
    // Do the Facebook sharing here
    let content = FBSDKSharePhotoContent()
    content.photo = ....
    ....
}))
zc246
  • 1,514
  • 16
  • 28
  • I see. I understand why you are a little confused. This was just an example, I want to just put in some prefilled text. – Hugo Smith Nov 10 '15 at 00:33
  • Don't know what are you actually want to ask for. How to just the Facebook API? Or just link the share button action with the popup? Or actually building up your own popupView? – zc246 Nov 10 '15 at 12:47
0

Here is an answer: I defined the share class as:

func share(action: UIAlertAction!){

    if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
        let facebook:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
        facebook.setInitialText("I just scored \(score) in Flagpoles.")

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

        score = 0

    } else {
        let alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)

        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)
    }
Hugo Smith
  • 11
  • 3