分享appView链接到ActivityViewController iOS swift?

时间:2016-03-08 07:59:56

标签: ios objective-c swift uiactivityviewcontroller

我的专业应用尚未上线。我的应用程序编号(XXXXXX)我从iTunes连接。现在我想分享应用程序链接到社交媒体(Whats App,其他应用程序)。我使用ActivityViewController我使用此代码

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

let shareItems:Array = [UIApplication.sharedApplication().openURL(url!)]

        let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
        self.presentViewController(activityViewController, animated: true, completion: nil)

问题:但它是社交应用程序(Whatsapp)。如何解决这个问题?

enter image description here

4 个答案:

答案 0 :(得分:3)

在这里你用过

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

这用于打开网站不共享

所以喜欢

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

   self.presentViewController(activityVC, animated: true, completion: nil)
}
else
{
 // show alert for not available
}

示例请参见this

答案 1 :(得分:1)

Swift 4解决方案:

此解决方案也可在iPad上使用(上述解决方案在iPad上崩溃):

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

            if UI_USER_INTERFACE_IDIOM() == .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)
        }

答案 2 :(得分:1)

这里的解决方案都很好,但值得考虑实施 UIActivityItemSource 协议和 LinkPresentation 框架。

我的解决方案实现了以下目标:

  • 显示在 UIActivityViewController 顶部的应用图标和标题
  • AirDrop 的直接链接到 App Store ActivityType
  • 消息和电子邮件的自定义文本,包括在需要时添加指向 Google Play 应用链接的机会
  • 电子邮件主题
  • 不使用 LPMetaDataProvider 获取请求(如本 WWDC 2019 262 视频中所述)加载速度如此之快

0。初始化 UIActivityViewController

将项目设置为 self

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

排除某些不适用的ActivityType

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

对于 iPad,设置 popoverPresentationController.sourceView.barButtonItem(这在 iPhone 上被忽略):

activityVC.popoverPresentationController?.sourceView = myButton

呈现:

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

1.实现所需的 UIActivityItemSource 方法

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

您必须根据文档实现占位符方法:

<块引用>

占位符对象不必包含任何真实数据,但应尽可能接近您打算提供的实际数据对象进行配置。

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

以及实际数据,返回 AirDrop 的应用链接和其他所有内容的文本:

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.实现主题方法

来自文档:

<块引用>

对于支持主题字段的活动,返回项目的主题。

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

3.实现 LPLinkMetaData 方法

来自文档:

<块引用>

返回元数据以显示在共享表的预览标题中。

@available(iOS 13.0, *)
func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
    let metadata = LPLinkMetadata()
    metadata.title = "APP_NAME"
    return metadata
}

答案 3 :(得分:0)

iPhone Swift 5+

解决方案
let url = URL(string: "https://apps.apple.com/us/app/id1535629801")!
let vc = UIActivityViewController(activityItems: [url], applicationActivities: nil)
present(vc, animated: true)