在Swift中向UIAlertController添加共享操作

时间:2015-11-09 22:24:14

标签: ios swift

我正在使用UIAlertController构建一个应用程序,我希望有一个按钮,表示"分享"然后打开像

这样的东西

this

如果它不是一个子按钮,它似乎很容易做到。

我的代码

}


@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

    }

目前尚未定义我的共享功能。

3 个答案:

答案 0 :(得分:2)

您可以在完成处理程序中定义在点击UIActivityController后打开UIAlertAction,但默认情况下,iPhone中的Apple不会在iPad中显示所需的份额。使用以下代码,您可以在点击按钮后显示共享:

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)

在上面的代码中,我总结了添加更多按钮,我假设你想要iPhone的代码,如果你也想要iPad,你可以在这个问题中检查我的答案:

我希望这对你有所帮助。

答案 1 :(得分:0)

如果您的share方法不起作用,请执行此类操作

ac.addAction(UIAlertAction(title: "Share", style: .Default, handler: { (action) -> Void in 
    // Do the Facebook sharing here
    let content = FBSDKSharePhotoContent()
    content.photo = ....
    ....
}))

答案 2 :(得分:0)

以下是答案: 我将共享类定义为:

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)
    }