共享任何内容时,Apple 都会获得共享链接的图像和标题。这是相当准确的。有什么办法可以获取这些数据吗?
我尝试使用
OpenGraph
数据,这适用于某些网站,但不适用于所有网站。
Apple有提供获取图片和标题的函数吗?我找不到任何关于此的信息。
您可以像这样从 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&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>