如何将 UIActivity Copy 更改为在 Swift 中的 UIActivityViewController 中复制 Link

How to change UIActivity Copy to copy Link in UIActivityViewController in Swift

当共享的项目是 link

时,如何更改要复制的文本 link

我已经试过了,但没有达到预期效果


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

这里附上了我所期望的截图

“复制”是系统默认提供的项目,您无法更新,因为 UIActivity.activityTitle 是只读的。

不过,你可以添加自定义的activity项目,你几乎做到了,下面是我的版本

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

您可以调用初始化此 CustomUIActivity,如下所示:

    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)

这将起作用,并复制 link: