Icons from assets and system icons display in UIAlertController actions

34 Views Asked by At

I can't figure out how to display asset image as an icon for action. And how to display a system image (SF-symbol) as icon for share item. Although vice versa everything works like a charm. Here is the code to create UIActivity on the fly:

class ViewController: UIViewController {

    @IBAction func shareAction(_ sender: Any) {
        
        let items:[Any] = [URL(string: "example.com")!]
        let iconFromAssets = UIImage(named: "qr-code")
        let iconFromAssetsShare = ShareActivity(title: "Asset icon QR (share)", image: iconFromAssets) { _ in }
        let iconFromAssetsAction = ActionActivity(title: " Asset icon QR (action)", image: iconFromAssets) { _ in }
        let systemIcon = UIImage(systemName: "qrcode")
        let systemIconShare = ShareActivity(title: "System icon QR (share)", image: systemIcon) { _ in }
        let systemIconAction = ActionActivity(title: "System icon QR (action)", image: systemIcon ) { _ in }
        
        let avc = UIActivityViewController(activityItems: items,
                                           applicationActivities: [
                                            iconFromAssetsShare,
                                            iconFromAssetsAction,
                                            systemIconShare,
                                            systemIconAction
                                           ])
        avc.excludedActivityTypes = [
            .airDrop,
            UIActivity.ActivityType(rawValue: "com.apple.reminders.sharingextension")]
        
        present(avc, animated: true, completion: nil)
    }
    
}

where ActionActivity and ShareActivity are:

class ActionActivity: UIActivity {
    
    var title: String
    var image: UIImage?
    var items:[Any] = [Any]()
    var action:([Any]) -> Void
    
    init(title: String, image: UIImage?, action: @escaping ([Any]) -> Void) {
        self.title = title
        self.image = image
        self.action = action
        super.init()
    }
    
    override var activityTitle: String? {
        return self.title
    }
    
    override var activityImage: UIImage? {
        return self.image
    }
    
    override var activityType: UIActivity.ActivityType? {
        return UIActivity.ActivityType("myactivity.activity.\(title)")
    }
    
    override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
        return true
    }
    
    override func prepare(withActivityItems activityItems: [Any]) {
        self.items = activityItems
    }
    
    override func perform() {
        action(self.items)
        activityDidFinish(true)
    }
}

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

Here is how the result looks.

enter image description here

How do I make an icon out of system image for share action category and icon for action from image from asset?

I was expecting system and asset images could serve as icons.

0

There are 0 best solutions below