I am using UIActivityViewController
to share a webview url on iOS 11
and when using Copy
then go to Messages app then paste, it is pasted twice.
Out of curiosity I tried to do the same with Safari, Chrome, Firefox, and also for other apps than messages. The result was interesting:
Here is my simple code
func shareURL(title: String, url: URL) {
var activityItems = [AnyObject]()
activityItems.append(TitleActivityItemProvider(title: title))
activityItems.append(url as AnyObject)
let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
}
and here is the TitleActivityItemProvider
class
class TitleActivityItemProvider: UIActivityItemProvider {
static let activityTypesToIgnore = [UIActivityType.copyToPasteboard]
init(title: String) {
super.init(placeholderItem: title)
}
override var item : Any {
if let activityType = activityType {
if TitleActivityItemProvider.activityTypesToIgnore.contains(activityType) {
return NSNull()
}
}
return placeholderItem! as AnyObject
}
override func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivityType?) -> String {
return placeholderItem as! String
}
}
is it a bug on iOS 11
or should I consider doing specific change when working with UIActivityViewController
==UPDATE==
I noticed when I comment adding TitleActivityItemProvider
it works fine and when I add it, it duplicates the url however I ignore UIActivityType.copyToPasteboard
in the title provider and return NSNull()
So I fixed it by using url.absoluteString
when adding item to activityItems
activityItems.append(url.absoluteString as AnyObject)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With