0

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

5
  • try to show us what you have tried Commented Jul 17, 2020 at 10:50
  • Yes, tried but it's not come as I expect, I have updated my question Commented Jul 17, 2020 at 11:45
  • It seems that you got your answer Commented Aug 25, 2020 at 19:56
  • Yes, I got my answer Commented Aug 26, 2020 at 11:14
  • nice to hear that Commented Aug 27, 2020 at 5:08

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

Another solution is to define a custom UIActivityItemSource, like this:

class MyCustomItemSource: NSObject, UIActivityItemSource { private let propertyA: URL private let propertyB: String init(a: URL, b: String) { self.propertyA = a self.propertyB = b } func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? { switch activityType { case .copyToPasteboard: return a default: return "\(a.absoluteString)\nb" } } func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any { return a } } 

Then, use it as below:

let myItemSource = MyCustomItemSource(a: URL(string: "your url"), b: "Your text to share") let activityVC = UIActivityViewController(activityItems: [myItemSource], applicationActivities: []) vc.present(activityVC, animated: true) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.