如何添加此共享按钮? IOS8用迅捷 [英] How do i add this sharing button? IOS8 with swift

查看:242
本文介绍了如何添加此共享按钮? IOS8用迅捷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want there to be a button in my app, that when it is pressed, this(See image below) Pops up. How do i do that? I don't want to create a custom sharing extion, i just want the default one? What code do i use? All the tutorials online are in objective-c. Please give an answer in 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服务为什么?该操作是否连接到导航栏按钮项?

Here is my code so far, but i get an error that UIBarButtonItem Is not convetable to UIVIew Why? The action is connected to a navigation bar button item?

@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 ,例如通过以下方式:

The way is using UIActivityViewController for example in the following way :

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

The above code works for both iPhone and iPad because in you set the new popoverPresentationController in iOS 8 it works for iPad too.

如果使用 UIBarButtonItem 你需要替换这一行:

In the case of use an UIBarButtonItem you need to replace this line:

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

这一个:

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

我希望这能帮到你。

这篇关于如何添加此共享按钮? IOS8用迅捷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆