如何添加此共享按钮? IOS8用迅捷

时间:2015-05-07 20:40:10

标签: iphone swift ios8 sharing ios-extensions

我希望我的应用程序中有一个按钮,当按下它时,会弹出这个(见下图)。我怎么做?我不想创建自定义共享,我只想要默认的?我用什么代码?所有在线教程都是客观的。请快速给出答案。

图片: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)



}

1 个答案:

答案 0 :(得分:6)

方法是使用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)

我希望这对你有所帮助。