7

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:

  • Coping from Safari always works in any app
  • Coping from Chrome or FireFox works in Notes app, but it duplicates the copied text in any app that has TextField (Messages, WhatsApp, Slack, Signal,...)

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

Mahmoud Adam
  • 5,772
  • 5
  • 41
  • 62

2 Answers2

1

So I fixed it by using url.absoluteString when adding item to activityItems

activityItems.append(url.absoluteString as AnyObject)
Mahmoud Adam
  • 5,772
  • 5
  • 41
  • 62
0

Are you testing on the Simulator? On the simulator I had this issue, but on device it only pastes once.