为UIActivityViewController Swift设置不同的活动项目 [英] Set different activity items for UIActivityViewController Swift

查看:107
本文介绍了为UIActivityViewController Swift设置不同的活动项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 UIActivityViewController ,有4个选项:消息/邮件/ TW / FB
我想为每个选项发送不同的活动!

I have UIActivityViewController with 4 options: Message / Mail / TW / FB. I want to send different activities for each option!

例如:
In iMessage 表格,我需要把:字符串 NSURL ,以及 UIImage
在邮件中我需要在主题字段中放置字符串,然后在正文中放置字符串, UIImage NSURL 也。
TW / FB 我想放置像社交网站的图片,还有一些字符串和 NSURL

For eg: In iMessage sheet, I need to put : String, NSURL, and UIImage. In Mail I need to place string in Subject field, then String in body, UIImage and NSURL also. In TW/FB I want to place image like socials post did it, also some String and NSURL.

您是否知道在iOS8中是否可以使用Swift?

Do you have any idea if this is possible in iOS8, with Swift ?

我搜索了很多对于某些代码,没有找到最适合我。

I searched a lot for some pieces of code, did not found the best for me.

推荐答案

你应该利用 UIActivityItemSource 协议。 UIActivityViewController 的初始化程序的 activityItems 参数接受数据对象数组或实现<的对象数组code> UIActivityItemSource protocol。

You should take advantage of the UIActivityItemSource protocol. The activityItems parameter of the initializer of UIActivityViewController accepts either an array of data objects or an array of objects that implement the UIActivityItemSource protocol.

作为示例,请考虑以下项目来源。

As an example consider an item source like the following.

class MyStringItemSource: NSObject, UIActivityItemSource {
    @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
        return ""
    }

    @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
        if activityType == UIActivityTypeMessage {
            return "String for message"
        } else if activityType == UIActivityTypeMail {
            return "String for mail"
        } else if activityType == UIActivityTypePostToTwitter {
            return "String for twitter"
        } else if activityType == UIActivityTypePostToFacebook {
            return "String for facebook"
        }
        return nil
    }

    func activityViewController(activityViewController: UIActivityViewController, subjectForActivityType activityType: String?) -> String {
        if activityType == UIActivityTypeMessage {
            return "Subject for message"
        } else if activityType == UIActivityTypeMail {
            return "Subject for mail"
        } else if activityType == UIActivityTypePostToTwitter {
            return "Subject for twitter"
        } else if activityType == UIActivityTypePostToFacebook {
            return "Subject for facebook"
        }
        return ""
    }

    func activityViewController(activityViewController: UIActivityViewController, thumbnailImageForActivityType activityType: String!, suggestedSize size: CGSize) -> UIImage! {
        if activityType == UIActivityTypeMessage {
            return UIImage(named: "thumbnail-for-message")
        } else if activityType == UIActivityTypeMail {
            return UIImage(named: "thumbnail-for-mail")
        } else if activityType == UIActivityTypePostToTwitter {
            return UIImage(named: "thumbnail-for-twitter")
        } else if activityType == UIActivityTypePostToFacebook {
            return UIImage(named: "thumbnail-for-facebook")
        }
        return UIImage(named: "some-default-thumbnail")
    }
}

上述项目源根据活动类型返回不同的字符串数据对象,主题和缩略图。要使用,您只需将其传递到 UIActivityViewController 初始值设定项。

The above item source returns different string data objects, subjects and thumbnail images based on the activity type. To use, you just need to pass it into the UIActivityViewController initializer.

UIActivityViewController(activityItems: [MyStringItemSource()], applicationActivities: nil)

同样,你可以定义一个自定义 MyUrlItemSource 类,它根据所选活动返回不同的URL并在初始化程序中传递它。

Similarly, you could define a custom MyUrlItemSource class that returns different URLs based on the selected activity and pass it along in the initializer.

UIActivityViewController(activityItems: [MyStringItemSource(), MyUrlItemSource()], applicationActivities: nil)

所有这些都在 UIActivityViewController UIActivityItemSource

All of this is outlined in the official documentation for UIActivityViewController and UIActivityItemSource

这篇关于为UIActivityViewController Swift设置不同的活动项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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