如何添加此分享按钮? IOS8 与 swift

Posted

技术标签:

【中文标题】如何添加此分享按钮? IOS8 与 swift【英文标题】:How do i add this sharing button? IOS8 with swift 【发布时间】:2015-05-07 20:40:10 【问题描述】:

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

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

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

希望对你有所帮助。

【讨论】:

请不要再阅读我的答案,我把 cmets 放在 iPad 上,两者都适用 好的,我明白了。如何为其添加更多功能,例如 facebook 和 twitter? 应用程序在使用 ipad 时崩溃。为什么? activityViewController.popoverPresentationController?.sourceView = (sender as!UIButton) Facebook 和 Twitter 可以很好地使用我在上面放置的代码并共享文本、网址和图像。但是要小心 Facebook,因为如果下载应用程序会遇到一些麻烦等。它只会发生在 Facebook 上,因为他的政策发生了变化!

以上是关于如何添加此分享按钮? IOS8 与 swift的主要内容,如果未能解决你的问题,请参考以下文章

iOS/Swift:如何在 AVPlayerViewController 的播放控件中添加共享按钮?

如何使用代码在 Swift iOS8 中模拟按钮按下

如何在 phonegap 中添加社交分享按钮

如何使用 Swift XCUITest 添加摇晃手势? [关闭]

如何向子视图属性 Swift 添加操作? [复制]

swift spritekit Facebook 分享按钮