如何自定义UIActivityViewController以仅包括自定义UIActivity类型? [英] How can you customize UIActivityViewController to ONLY include custom UIActivity types?

查看:363
本文介绍了如何自定义UIActivityViewController以仅包括自定义UIActivity类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自定义本机ios UIActivityViewController,因此它将限制每个UIActivity,但我用来初始化UIActivityViewController的自定义UIActivity类型除外.

I'm trying to customize the native ios UIActivityViewController so it will restrict every UIActivity except for the custom UIActivity types that I initialize the UIActivityViewController with.

基本上,我只希望用户看到我的四个自定义UIActivity.

Essentially, I only want the user to see my four custom UIActivitys.

let items = ["Hello world"]

let activitySheet = CustomUIActivityViewController(
            activityItems: items,
            applicationActivities: [
                CustomMailUIActivity(),
                CustomMessagesUIActivity(),
                CustomTwitterUIActivity(),
                CustomFacebookUIActivity()
            ]
        )

我知道您可以使用activitySheet.excludedActivityTypes = []来排除不需要的类型,但是您不能排除诸如Slack之类的第三方应用程序.

I understand that you can use activitySheet.excludedActivityTypes = [] to exclude types that you do not need, however you are not able to exclude third party applications such as Slack.

我想知道是否有任何方法可以解决此问题,并且仅包含自定义applicationActivies.最好也删除显示在共享表上的更多"按钮.任何帮助将不胜感激.

I was wondering if there was any way to get around that and ONLY include custom applicationActivies. It would also be great to remove the "More" button that appears on the share sheet as well. Any help would be greatly appreciated.

这就是我要实现的目标.

This is what I'm trying to achieve.

屏幕截图

推荐答案

我的同事能够弄清楚这一点.对于想知道如何在共享表单上仅显示 (自定义活动)的任何人,这就是我的方法.

My colleague was able to figure this out. For anyone wondering how to display only the applicationActivities (custom activities) on the share sheet, this is how I did it.

对于activityItems,而不是创建字符串数组,而是创建自定义数据对象的数组.然后在自定义UIActivity(UIActivity的子类)中覆盖canPerform(withActivityItems activityItems: [Any])方法,如果activityItems[o] is CustomItem返回true.由于您的activityItems是自定义的,因此系统不会在共享表上显示其他应用程序.就是这样.

For activityItems rather than creating an array of strings, create an array of your custom data objects. Then override the canPerform(withActivityItems activityItems: [Any]) method in your custom UIActivity (subclass of UIActivity) to return true if activityItems[o] is CustomItem. Since your activityItems are custom, system will not display other apps on the share sheet. That's it.

在下面的示例中,只有CustomUIActivity将显示在共享表上.

In the example below, only your CustomUIActivity will be displayed on the share sheet.

let items = [CustomItem("Hello world")]

let activitySheet = UIActivityViewController(
            activityItems: items,
            applicationActivities: [
                CustomUIActivity()
            ]
        )

class CustomItem {

    let value: String

    required init(value: String) {
        self.value = value
    }

    func getValue() -> String {
        return self.value
    }
}

@objc(CustomUIActivity)
class CustomUIActivity: UIActivity {

    override class var activityCategory: UIActivity.Category {
        return .share
    }

    override var activityType: UIActivity.ActivityType? {
        return .customuiactivity
    }

    override var activityTitle: String? {
        return "Custom"
    }

    override var activityImage: UIImage? {
        return UIImage(named: "custom-icon")
    }

    override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
        if activityItems.count == 1 && activityItems[0] is CustomItem {
            return true
        }
        return false
    }

    var textToShare: String?

    override func prepare(withActivityItems activityItems: [Any]) {
        if let activityItem = activityItems.first as? CustomItem {
            self.textToShare = activityItem.getValue
        }
    }

    override func perform() {
        // perform your custom activity with `textToShare`
        activityDidFinish(true)
    }
}

extension UIActivity.ActivityType {
    static let customuiactivity =
        UIActivity.ActivityType("com.ceylonese.mobile.customuiactivity")
}

这篇关于如何自定义UIActivityViewController以仅包括自定义UIActivity类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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