Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中将共享操作添加到UIAlertController_Ios_Swift - Fatal编程技术网

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

Ios 在Swift中将共享操作添加到UIAlertController,ios,swift,Ios,Swift,我正在构建一个带有UIAlertController的应用程序,我希望有一个显示“共享”的按钮,然后打开类似 这似乎很容易做到,如果它不是一个子按钮 我的代码: 我的共享功能当前未定义。如果您的共享方法不起作用,请执行类似操作 ac.addAction(UIAlertAction(title: "Share", style: .Default, handler: { (action) -> Void in // Do the Facebook sharing here

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

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

我的代码:


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

如果您的
共享方法不起作用,请执行类似操作

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

您可以在完成处理程序中定义在点击
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的代码,您可以在以下问题中查看我的答案:


我希望这对你有帮助

这里有一个答案: 我将股份类别定义为:

func共享(操作:UIAlertAction!){


你在哪里定义你的共享操作的完成处理程序?我明白了。我理解你为什么有点困惑。这只是一个例子,我只想输入一些预填充的文本。不知道你到底想要什么。如何使用Facebook API?或者只是将共享按钮操作与弹出窗口链接?或者实际构建你的应用程序弹出视图?
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)
    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)
    }