如何添加此共享按钮? IOS8 和 swift

How do i add this sharing button? IOS8 with swift

我想在我的应用程序中有一个按钮,当它被按下时,这个(见下图)弹出。我怎么做?我不想创建自定义共享扩展,我只想要默认的?我使用什么代码?网上所有教程都在objective-c。请在swift.

中给出答案

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

到目前为止,这是我的代码,但我收到一个错误,提示 UIBarButtonItem 不可转换为 UIVIew 为什么?该操作连接到导航栏按钮项?

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



}

使用 UIActivityViewController 的方式如下:

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

以上代码适用于 iPhone 和 iPad,因为在 iOS 8 中设置新的 popoverPresentationController 它也适用于 iPad。

在使用 UIBarButtonItem 的情况下,您需要替换此行:

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

有了这个:

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

希望对你有所帮助。