OG 1.3.3

OG 1.3.3

测试已测试
语言语言 SwiftSwift
许可证 BSD
发布最新发布2017 年 9 月
SwiftSwift 版本3.0
SPM支持 SPM

Zachary Drayer 维护。



OG 1.3.3

  • 作者
  • Zachary Drayer

这是什么?

OG 是 Swift 中的 OpenGraph 解析器。

什么是 OpenGraph?

您知道在 Facebook 或 Twitter 上出现的网站智能预览吗?OpenGraph 是用于实现此功能的规范。

这包含哪些内容?

包括一个包含用于动态库的 Xcodeproj 的 Xcode Workspace 和一个用于实验的 Xcode Playground。

我需要什么来使用这个?

要求包括 iOS 8、Mac OS X 10.9、tvOS 9.0 或 watchOS 2.0、Swift 3.0(包含在 Xcode 8.x 中)。

我该如何开始使用这个?

要开始使用 OG,您可以通过以下方式将其添加到项目中

  • 使用 Carthage,并在 Cartfile 中添加 github "zadr/OG" ~> 1.3.2
  • 使用 CocoaPods,并在 Podfile 中添加 pod 'OG', '~1.3.2
  • 将当前仓库作为 git submodule 添加,将 OG.xcodeproj 添加到您的 xcodeproj 中,并将 OG.framework 作为嵌入式二进制文件添加到项目的 Target 中。

如果我需要帮助或想要帮助怎么办?

如果您需要任何帮助或发现了一个错误,请 提出一个问题!如果您想修复错误或做出任何其他贡献,请随时提出问题,提交一个 pull request更新 wiki 中您发现有用的任何内容。

使用这个是什么样的?

使用 OG 有两种方式。

第一种方式是从 URL(或 URLRequest)自动检索元数据

if let url = URL(string: "https://…") {
    url.fetchOpenGraphData { (metadata) in
        print(metadata)
    }
}

第二种方式更注重手动操作;OG不是自动抓取、解析和跟踪标签,而是暴露了每个步骤所使用的组件,让您可以根据需要选择。

// first, fetch data from the network
if let url = URL(string: "https://…") {
    // first fetch html that might have opengraph previews
  // The demo uses the built-in `URLSession`, but anything that can fetch data can be used here 
  let task = URLSession.shared.dataTask(with: url) { (data, response, error)
        // make sure we successfully completed a request
      if let response = response as? HTTPURLResponse, response.statusCode >= 200, response.statusCode < 300 {
            if let data = data, let html = String(data: data, encoding: .utf8) {
                parse(html: html)
            }
        }
    }

    task.resume()
}
// and then parse OpenGraph meta tags out of an html document
func parse(html: String) {
    // then create a parser that can tell us the contents of each html tag and any associated key/value properties it has
  // `Parser` is provided, but this can be substituted with anything else that can iterate through html tags 
  let parser = Parser()

    // and keep track of each <meta class="og:…"> tag as the parser encounters it
  // This could also be replaced with another component, but outside of testing purposes, there's less of an obvious need to do so than with the other steps of the process.
  let tagTracker = TagTracker()
    parser.onFind = { (tag, values) in
        if !tagTracker.track(tag, values: values) {
            print("refusing to track non-meta tag \(tag) with values \(values)")
        }
    }

    if parser.parse(html) {
        // - If we can parse html, map over our results to go from arrays of arrays of dictionaries (`[[String: OpenGraphType]]`)
      // to an array of OpenGraph objects.
      // - Note: OpenGraph can have multiple elements on a page (for example, an og:article, follwed by an og:author, followed by another og:author)
      let tags = tagTracker.metadatum.map(Metadata.from)
        print(tags)
    }
}

之后我该做什么呢?

可能是在屏幕上展示网站的预览。为了帮助您做到这一点,每个OpenGraph Metadata对象都有一个 title、一个 imageUrl和一个在点击或触摸时打开的 url