dart 从ShareIntent获取共享图像和标题信息

tsm1rwdh  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(41)

当分享任何东西时,苹果会为共享链接获得一个图像和一个标题。这是相当准确的。有什么方法可以获得这些数据吗?
x1c 0d1x的数据
我尝试使用OpenGraph-data,这对一些网站有效,但不是所有网站。
苹果有没有提供获取图片和标题的功能?我在这上面找不到任何东西。

j8ag8udp

j8ag8udp1#

你可以像这样从URL中获取数据:

if let url = URL(string: "https://stackoverflow.com/") {
    URLSession.shared.dataTask(with: url) { data, resp, error in
        if let error = error {
            print(error.localizedDescription)
        }
        if let data = data {
            let str = String(data: data, encoding: .utf8)
            print(str as Any)
        }
    }
    .resume()
}

字符串
您可以从下面的字符串中突出显示的行中获取标题和图像:

<!DOCTYPE html>
             <html>
             <head>

EXTRACT TITLE FROM HERE --->
             <title>Stack Overflow</title>

EXTRACT ICON FROM HERE (href link) ---> 
                 <link rel="shortcut icon" href="//cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico?v=4f32ecc8f43d">
                 <link rel="apple-touch-icon image_src" href="//cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon.png?v=c78bd457575a">
                 <link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="/opensearch.xml">
                 <meta name="twitter:card" content="summary">
                 <meta name="twitter:domain" content="stackoverflow.com"/>
                 <meta property="og:type" content="website" />

                 <meta property="og:image" itemprop="image primaryImageOfPage" content="http://cdn.sstatic.net/Sites/stackoverflow/img/[email protected]?v=73d79a89bded&a" />
                 <meta name="twitter:title" property="og:title" itemprop="title name" content="Stack Overflow" />
                 <meta name="twitter:description" property="og:description" itemprop="description" content="Q&amp;A for professional and enthusiast programmers" />
                 <meta property="og:url" content="http://stackoverflow.com/"/>

                 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
                 <script src="//cdn.sstatic.net/Js/stub.en.js?v=b84e3ec1d0b3"></script>
                 <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/Sites/stackoverflow/all.css?v=ef9c49b839e0">

                 <link rel="alternate" type="application/atom+xml" title="Feed of recent questions" href="/feeds">
                 

                 <script>

相关问题