Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
712 views
in Technique[技术] by (71.8m points)

ios - How can you customize UIActivityViewController to ONLY include custom UIActivity types?

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.

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()
            ]
        )

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.

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.

screenshot

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.

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")
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...