Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - How to share app link as in the App Store

I'm trying to share my app using UIActivityViewController but I can't reproduce the same effect as when I share an app from the App Store, meaning :

When I clicked the share button in the App Store I have something that looks like this :

enter image description here

But when I try to share my app I have this :

enter image description here

The code that I used was :

if let logo = UIImage(named: "myLogo"), let websiteURL = URL(string: "https://itunes.apple.com/app/idxxxxxxxxxx") {
   let objectsToShare = ["My App Name", websiteURL, logo] as [Any]
   let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: [])
   if let popoverController = activityVC.popoverPresentationController {
      popoverController.sourceView = self.view
      popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
   }
   present(activityVC, animated: true)
}

The if let popoverController = ... loop is for preventing a crash when using iPads.

What do I have to change in order to have the effect as the App Store? (to have an image with a title and a subtitle)

Moreover, once I share the app with Messages for instance, this is the difference :

enter image description here enter image description here

How can I have the same effect? (A single image with the title and subtitle, an as a bonus, a video). I'm not sure if this is an iOS 13 problem, since all similar questions don't have the same app sharing popover.

like image 290
NicoC Avatar asked Nov 15 '22 13:11

NicoC


1 Answers

You have to use the new LinkPresentation framework.

Which essentially involves UIActivityItemSource conformance then retrieving the metadata that will encompass the Activity View and the data you are sharing. Data can be retrieved locally or downloaded.

ExampleController: UIViewController { 
    var metadata: LPLinkMetadata?
    func share() {
        let activityView = UIActivityViewController(activityItems: [self], applicationActivities: nil)
        present(activityView, animated: true)
    }
    ...
}

extension ExampleController: UIActivityItemSource {

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

    func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
        return metadata
    }

    func activityViewControllerLinkMetadata(_: UIActivityViewController) -> LPLinkMetadata? {
        metadata = LPLinkMetadata()
        metadata.title = "Title"
        metadata.originalURL = URL(string: "Description")
        metadata.url = metadata.originalURL
        // Using a locally stored item
        metadata.iconProvider = NSItemProvider(object: UIImage(named: "image")!)
        metadata.imageProvider = NSItemProvider.init(contentsOf:
        Bundle.main.url(forResource: "image", withExtension: "JPG"))
        return metadata
    }
}

Docs: https://developer.apple.com/documentation/uikit/uiactivityitemsource/3144571-activityviewcontrollerlinkmetada

WWDC Presentation: https://developer.apple.com/videos/play/wwdc2019/262/

like image 60
Stefan Avatar answered Jan 01 '23 07:01

Stefan