Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share app link to by ActivityViewController iOS swift?

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

like image 263
Avijit Nagare Avatar asked Mar 08 '16 07:03

Avijit Nagare


2 Answers

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)
}
like image 174
Jochen Holzer Avatar answered Oct 19 '22 23:10

Jochen Holzer


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

like image 38
Anbu.Karthik Avatar answered Oct 20 '22 00:10

Anbu.Karthik