0
votes

How can I change the text to copy link when the item being shared is a link

I am tried this but it does not work as expected


    @objc(CustomUIActivity)
    class CustomUIActivity: UIActivity {
    
        private var url = NSURL()
    
        override class var activityCategory: UIActivity.Category {
            return .share
        }
    
        override var activityType: UIActivity.ActivityType? {
            return .customuiactivity
        }
    
        override var activityTitle: String? {
            return "Copy Link"
        }
    
        override var activityImage: UIImage? {
            return UIImage(named: "icon-copy")
        }
    
        override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
            for activityItem in activityItems {
                if let _ = activityItem as? NSURL {
                    return true
                }
            }
            return false
        }
    
        var textToShare: String?
    
        override func prepare(withActivityItems activityItems: [Any]) {
            for activityItem in activityItems {
                if let url = activityItem as? NSURL {
                    self.url = url
                }
            }
        }
    
        override func perform() {
            // perform your custom activity
            UIPasteboard.general.string = url.absoluteString
            activityDidFinish(true)
        }
    }
    
    
    extension UIActivity.ActivityType {
        static let customuiactivity =
            UIActivity.ActivityType("com.productHunt.copyLink")
    }
    

ScreenShot

Here I have attached screenshot what I have expected

1
try to show us what you have triedAAEM
Yes, tried but it's not come as I expect, I have updated my questionJayaseelan Kumarasamy
It seems that you got your answerAAEM
Yes, I got my answerJayaseelan Kumarasamy
nice to hear thatAAEM

1 Answers

2
votes

The "Copy" is the default system provided item that you cannot updated as UIActivity.activityTitle is read only.

However, you can add the custom activity item, you have almost did the same below is my version

class CustomUIActivity : UIActivity
{
    var _activityTitle: String
    var _activityImage: UIImage?
    var activityItems = [Any]()
    var action: ([Any]) -> Void
    private var url = URL(string: "Nil")

    init(title: String, image: UIImage?, performAction: @escaping ([Any]) -> Void) {
        _activityTitle = title
        _activityImage = image
        action = performAction
        super.init()
    }

    override var activityTitle: String? {
        return _activityTitle
    }

    override var activityImage: UIImage? {
        return _activityImage
    }

    override var activityType: UIActivity.ActivityType? {
        return UIActivity.ActivityType(rawValue: "com.productHunt.copyLink")
    }

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

    override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
        for activityItem in activityItems {
           if let _ = activityItem as? URL {
              return true
           }
        }
        return false
    }

   override func prepare(withActivityItems activityItems: [Any]) {
       for activityItem in activityItems {
           if let url = activityItem as? URL {
               self.url = url
           }
       }
       self.activityItems = activityItems
   }

   override func perform() {
    print("URL : \(String(describing: url?.absoluteString))")
    UIPasteboard.general.string = url?.absoluteString
    action(activityItems)
    activityDidFinish(true)
   }
}

You can call initialize this CustomUIActivity as below:

    let customItem = CustomUIActivity(title: "Copy Link", image: UIImage(named: "icon-copy")) { sharedItems in
        for string in sharedItems {
            print("Here's the string: \(string) ")
        }
    }

    let items: [Any] = ["This app is my favorite", URL(string: "https://www.google.com")!]
    let ac = UIActivityViewController(activityItems: items, applicationActivities: [customItem])
    present(ac, animated: true)

This will work, and copies the link:

enter image description here