Home > Mobile >  How to show URL icon with text in UIActivityViewController?
How to show URL icon with text in UIActivityViewController?

Time:10-17

I have tried different ways to show a URL icon with text but didnt succeed. Here is the code and its output snippets

Example 1:

let text = "I am text"
let myWebsite = URL(string:"https://www.youtube.com/")
let shareAll = [text, myWebsite] as [Any]
let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)

Output 1: UIActivity View Messages View

Example 2:

let myWebsite = URL(string:"https://www.youtube.com/")
let shareAll = [myWebsite] as [Any]
let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)

Output 2:

UIActivity View

Required: I want URL icon like in Output 2 for Example 1.

CodePudding user response:

There is a difference between sharing data and showing data - I think in your case, showing the data that's about to be shared.

I could be wrong, but if not, you are actually able to share the data - in this case a UIImage and text - but wish to display what is about to be shared. I had this issue a while back. Here's my code. See if it can help you - basically you need to add metadata to UIActivityViewController delegate.

func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
    let shareImage = preview.uiImage.adjustedForShareSheetPreviewIconProvider()
    let imageProvider = NSItemProvider(object: shareImage)
    let metadata = LPLinkMetadata()
    metadata.imageProvider = imageProvider
    metadata.title = "Title"
    return metadata
}

Adjust shareImage and title as needed.

EDIT

I was using my code and was incomplete. adjustedForShareSheetPreviewIconProvider() is part of UIImage, and as such, it assumes this. In my code, I'm actually calling a GLKView to return this UIImage and then use it this way in the metadata.

So if your image is something returned from the URL as an icon and is (or can be converted to) a UIImage, just add it to the metadata like my code above.

  • Related