You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从其他应用分享的链接获取数据?实现YouTube式富链接展示

实现富链接分享与解析的方案

嘿,这个需求我太熟了!分两部分给你拆解清楚,都是实际项目里用过的干货:

一、让自有App的分享链接变成富链接(像YouTube那样)

核心原理是给你的网页链接添加Open Graph(OG)元标签,Facebook、WhatsApp这类社交App会自动爬取这些标签,生成带标题、缩略图的富链接。

1. 给网页链接配置OG标签

在你要分享的网页的HTML头部,添加以下meta标签(服务器端渲染或者静态页面都可以):

<head>
  <!-- 核心OG标签 -->
  <meta property="og:title" content="你的内容标题" />
  <meta property="og:description" content="内容的简短描述,别太长" />
  <meta property="og:image" content="https://你的服务器地址/缩略图.jpg" />
  <meta property="og:url" content="https://你的固定链接地址" />
  <meta property="og:type" content="article" /> <!-- 根据内容类型选,比如video、website -->
  
  <!-- 可选:针对WhatsApp的优化,有些场景需要 -->
  <meta property="og:site_name" content="你的App名称" />
</head>

注意点:

  • 缩略图要符合平台要求:比如Facebook要求至少1200x630px,WhatsApp建议最小300x200px,格式用JPG/PNG
  • 确保你的服务器不会屏蔽社交平台的爬虫(别把FB、WhatsApp的爬虫IP段拦了)
  • 可以用平台自带的调试工具验证标签是否生效(输入链接就能看到富链接预览效果)

2. App内的分享实现

不管是Android还是iOS,直接分享上面的链接就行,社交App会自动处理:

  • Android:用Intent发送文本类型的分享,把链接传进去
    val shareIntent = Intent(Intent.ACTION_SEND).apply {
        type = "text/plain"
        putExtra(Intent.EXTRA_TEXT, "https://你的固定链接地址")
    }
    startActivity(Intent.createChooser(shareIntent, "分享到"))
    
  • iOS:用UIActivityViewController
    let shareLink = URL(string: "https://你的固定链接地址")!
    let activityVC = UIActivityViewController(activityItems: [shareLink], applicationActivities: nil)
    present(activityVC, animated: true)
    

二、从其他App分享的链接中获取数据

1. 接收分享的链接

Android端:

在Manifest里给接收分享的Activity配置Intent Filter:

<activity android:name=".ShareReceiveActivity">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
</activity>

然后在Activity里获取链接:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    if (intent.action == Intent.ACTION_SEND && intent.type == "text/plain") {
        val sharedLink = intent.getStringExtra(Intent.EXTRA_TEXT)
        sharedLink?.let {
            // 这里拿到了其他App分享的链接
        }
    }
}

iOS端:

在Info.plist里添加支持的分享类型,或者在AppDelegate里处理:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    // 如果是直接打开链接的场景,这里拿到url
    return true
}

// 或者用UIActivityViewController接收(如果你的App作为分享目标)
func presentShareSheet() {
    let activityVC = UIActivityViewController(activityItems: [], applicationActivities: nil)
    activityVC.completionWithItemsHandler = { activityType, completed, returnedItems, error in
        if let items = returnedItems, let sharedLink = items.first as? URL {
            // 拿到分享的链接
        }
    }
    present(activityVC, animated: true)
}

2. 解析链接里的富数据(标题、缩略图等)

拿到链接后,你需要自己爬取这个链接的HTML,解析里面的OG标签:

  • 发送网络请求获取网页HTML
  • 解析HTML中的meta标签,提取og:title、og:image等内容
  • 可以用第三方库简化解析(比如Android用Jsoup,iOS用SwiftSoup),或者自己写正则匹配简单处理

举个Android用Jsoup的例子:

val doc = Jsoup.connect(sharedLink).get()
val title = doc.select("meta[property=og:title]").attr("content")
val imageUrl = doc.select("meta[property=og:image]").attr("content")
val description = doc.select("meta[property=og:description]").attr("content")

这样就能拿到其他App分享链接对应的富内容了~

内容的提问来源于stack exchange,提问作者suja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:43:48