如何更改 UIActivity Copy 到 Swift 中的 UIActivityViewController 中复制链接

问题描述 投票:0回答:2

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

我尝试过这个,但它没有按预期工作


    @objc(自定义UI活动)
    类 CustomUIActivity: UIActivity {
    
        私有变量 url = NSURL()
    
        覆盖类 var ActivityCategory: UIActivity.Category {
            返回.分享
        }
    
        覆盖 var ActivityType: UIActivity.ActivityType? {
            返回.customuiactivity
        }
    
        覆盖 var ActivityTitle:字符串? {
            返回“复制链接”
        }
    
        覆盖 var ActivityImage: UIImage? {
            返回 UIImage(命名为:“图标复制”)
        }
    
        覆盖 func canPerform(withActivityItems ActivityItems: [Any]) -> Bool {
            对于 ActivityItems 中的 ActivityItem {
                如果让 _ = ActivityItem 为? NSURL {
                    返回真
                }
            }
            返回错误
        }
    
        var textToShare:字符串?
    
        覆盖 func 准备(withActivityItems ActivityItems:[任何]){
            对于 ActivityItems 中的 ActivityItem {
                如果让 url = ActivityItem 为? NSURL {
                    self.url = 网址
                }
            }
        }
    
        覆盖函数执行(){
            // 执行您的自定义活动
            UIPasteboard.general.string = url.absoluteString
            活动完成(真)
        }
    }
    
    
    扩展 UIActivity.ActivityType {
        静态让customuiactivity =
            UIActivity.ActivityType("com.productHunt.copyLink")
    }
    

ScreenShot

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

ios swift xcode swift3 swift4
2个回答
2
投票

“复制”是系统默认提供的项目,您无法更新它,因为 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)

这样就可以了,复制链接:


0
投票

另一个解决方案是定义一个自定义的

UIActivityItemSource
,如下所示:

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, b]
        }
    }

    func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
        return a
    }
}

然后,如下使用:

let myItemSource = MyCustomItemSource(a: URL(string: "your url"), b: "Your text to share")
let activityVC = UIActivityViewController(activityItems: [myItemSource], applicationActivities: [])
vc.present(activityVC, animated: true)
© www.soinside.com 2019 - 2024. All rights reserved.