9

My app is not Live yet. I got the app ID from the App Store Connect. I want to share the app link on social media apps. I used the UIActivityViewController:

let string1 = "itms-apps://itunes.apple.com/app/idXXXXXXX"
let url = NSURL(string: string1)

let shareItems = [UIApplication.sharedApplication().openURL(url!)]
    
let activityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)

Problem: It is not showing some social media apps like WhatsApp.

enter image description here

budiDino
  • 13,044
  • 8
  • 95
  • 91
Avijit Nagare
  • 8,482
  • 7
  • 39
  • 68

4 Answers4

22

Solution for Swift 4 or better:

This solution also works on iPad (the solution above crashes on iPad):

if let urlStr = NSURL(string: "https://apps.apple.com/us/app/idxxxxxxxx?ls=1&mt=8") {
    let objectsToShare = [urlStr]
    let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

    if UIDevice.current.userInterfaceIdiom == .pad {
        if let popup = activityVC.popoverPresentationController {
            popup.sourceView = self.view
            popup.sourceRect = CGRect(x: self.view.frame.size.width / 2, y: self.view.frame.size.height / 4, width: 0, height: 0)
        }
    }

    self.present(activityVC, animated: true, completion: nil)
}
Jochen Holzer
  • 1,598
  • 19
  • 25
  • 1
    The URL is changed to "https://apps.apple.com/us/app/id\(your app id)" – Ahmadreza Sep 21 '20 at 07:41
  • If you are not doing it in Viewcontroller, you can use vc by adding "vc: UIViewController" parameter to the function instead of using self. I used "activityVC" instead of self :) It didn't work, so I'm writing this comment. – Alperen ARICI Nov 12 '21 at 07:56
15

This is used to open the site, not to share the app:

[UIApplication.sharedApplication().openURL(url!)]

Do this instead:

if let name = URL(string: "https://itunes.apple.com/us/app/myapp/idxxxxxxxx?ls=1&mt=8"), !name.absoluteString.isEmpty {
  let objectsToShare = [name]
  let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
  self.present(activityVC, animated: true, completion: nil)
} else {
  // show alert for not available
}

for sample see this

budiDino
  • 13,044
  • 8
  • 95
  • 91
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • Can i use "itms-apps://itunes.apple.com/app/idXXXXXXX" in place of "https://itunes.apple.com/us/app/myapp/idxxxxxxxx?ls=1&mt=8" ? – Avijit Nagare Mar 08 '16 at 08:28
  • is this one for call back method , we diectly check the app is avilable in our device or not else , if you use this one `https://itunes.apple.com/us/app/myapp/idxxxxxxxx?ls=1&mt=8, it directly redirect to safari – Anbu.Karthik Mar 08 '16 at 08:30
  • i seen ?ls=1&mt=8 is changing for some app... i want app download directly form link – Avijit Nagare Mar 08 '16 at 08:36
7

The solutions here are all good but it's worth considering implementing the UIActivityItemSource protocol and LinkPresentation framework.

My solution achieves the following:

  • App icon and title showing at the top of the UIActivityViewController
  • Direct link to App Store for AirDrop ActivityType
  • Custom text for messages and emails, including an opportunity to add a link to the app on Google Play if required
  • Subject for emails
  • Doesn't use the LPMetaDataProvider fetch request (as described in this WWDC 2019 262 video) so faster to load

0. Init the UIActivityViewController:

Set the items to self:

let activityVC = UIActivityViewController(activityItems: [self], applicationActivities: nil)

Exclude certain ActivityTypes which don't apply:

activityVC.excludedActivityTypes = [.addToReadingList, .assignToContact, .markupAsPDF, .openInIBooks, .saveToCameraRoll]

For iPad set the popoverPresentationController.sourceView or .barButtonItem (this is ignored on iPhone):

activityVC.popoverPresentationController?.sourceView = myButton

Present it:

present(activityVC, animated: true, completion: nil)

1. Implement the required UIActivityItemSource methods

https://developer.apple.com/documentation/uikit/uiactivityitemsource

You must implement the placeholder method which according to the docs:

Placeholder objects do not have to contain any real data but should be configured as closely as possible to the actual data object you intend to provide.

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

And the actual data, returning a link to the app for AirDrop and text for everything else:

func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
    
    if activityType == .airDrop {
        return URL(string: "APP_STORE_URL")!
    }
    
    return "Check out the APP_NAME on the App Store: APP_STORE_URL or on the Google Play Store: PLAY_STORE_URL"
}

2. Implement the subject method

From the docs:

For activities that support a subject field, returns the subject for the item.

func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivity.ActivityType?) -> String {
    return "EMAIL_SUBJECT" // e.g. App name
}

3. Implement the LPLinkMetaData method

From the docs:

Returns metadata to display in the preview header of the share sheet.

@available(iOS 13.0, *)
func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
    let metadata = LPLinkMetadata()
    metadata.title = "APP_NAME"
    return metadata
}
Leon
  • 3,614
  • 1
  • 33
  • 46
  • I had a doubt. If we are sharing link, how can we get the - Add to Reading list option in share sheet for our app? I'm unable to get that option while sharing a link from my app using share sheet. – Sayalee Pote Feb 22 '21 at 07:31
  • @SayaleePoteAs well as manually excluding activity types the system will also remove those that aren't compatible. I don't know what sort of data the system is expecting for adding to reading list though, I would have thought a link would work but it doesn't. – Leon Feb 23 '21 at 10:31
  • 1
    Just fixed it. A link works. We need to make sure the return type for placeholder is a link for that option to appear. Initially was sending a link but return type for placeholder was an empty string which was causing the problem. – Sayalee Pote Feb 24 '21 at 06:09
2

iPhone solution for Swift 5+

let url = URL(string: "https://apps.apple.com/us/app/id1535629801")!
let vc = UIActivityViewController(activityItems: [url], applicationActivities: nil)
present(vc, animated: true)
budiDino
  • 13,044
  • 8
  • 95
  • 91