繁体   English   中英

在Swift中将共享操作添加到UIAlertController

[英]Adding a share action to a UIAlertController in Swift

我正在使用UIAlertController构建应用程序,我想要一个显示“共享”的按钮,然后打开类似

这个

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

我的代码

}


@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

    }

我的共享功能当前未定义。

您可以在完成处理程序中定义以在点击UIAlertAction之后打开UIActivityController ,但是默认情况下,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的代码,则可以查看以下问题的答案:

希望对您有所帮助。

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

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

这是一个答案:我将共享类定义为:

func share(操作: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)
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM