Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActivityViewController Copy copies item twice on iOS 11

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

like image 615
Mahmoud Adam Avatar asked Oct 02 '17 13:10

Mahmoud Adam


Video Answer


1 Answers

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

activityItems.append(url.absoluteString as AnyObject)
like image 200
Mahmoud Adam Avatar answered Oct 05 '22 09:10

Mahmoud Adam