【问题标题】:How to change UIActivity Copy to copy Link in UIActivityViewController in Swift如何在 Swift 中将 UIActivity 复制更改为复制 UIActivityViewController 中的链接
【发布时间】:2020-11-07 03:29:06
【问题描述】:

当共享的项目是链接时,如何更改文本以复制链接

我试过了,但它没有按预期工作

@objc(CustomUIActivity) 类CustomUIActivity:UIActivity { 私有变量 url = NSURL() 覆盖类 var activityCategory: UIActivity.Category { 返回 .share } 覆盖 var activityType: UIActivity.ActivityType? { 返回 .customuiactivity } 覆盖 var activityTitle:字符串? { 返回“复制链接” } 覆盖 var activityImage: UIImage? { 返回 UIImage(命名:“图标复制”) } 覆盖 func canPerform(withActivityItems activityItems: [Any]) -> Bool { 对于activityItems中的activityItem { 如果让 _ = activityItem 为? NSURL { 返回真 } } 返回假 } var textToShare:字符串? 覆盖 func prepare(withActivityItems activityItems: [Any]) { 对于activityItems中的activityItem { 如果让 url = activityItem 为? NSURL { self.url = 网址 } } } 覆盖函数执行(){ // 执行你的自定义活动 UIPasteboard.general.string = url.absoluteString 活动完成(真) } } 扩展 UIActivity.ActivityType { 静态让 customuiactivity = UIActivity.ActivityType("com.productHunt.copyLink") }

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

【问题讨论】:

  • 尝试向我们展示您的尝试
  • 是的,试过了,但没有如我所愿,我已经更新了我的问题
  • 看来你得到了答案
  • 是的,我得到了答案
  • 很高兴听到这个消息

标签: ios swift xcode swift3 swift4


【解决方案1】:

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

但是,你可以添加自定义活动项,你几乎做了相同的下面是我的版本

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)

这将起作用,并复制链接:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-14
    • 2010-12-30
    • 2019-02-05
    • 2018-12-03
    • 2021-09-28
    • 2016-01-22
    • 1970-01-01
    • 2018-05-15
    相关资源
    最近更新 更多