链接预览器,适用于 iOS、macOS、watchOS 和 tvOS
它能从 URL 创建预览,抓取所有信息,如标题、相关文本和图片。
索引
视觉示例
UTF-8 | 扩展 UTF-8 | 图库 |
---|---|---|
![]() |
![]() |
![]() |
视频网站 | 图片 | |
![]() |
![]() |
要求和详情
- iOS 8.0+ / macOS 10.11+ / tvOS 9.0+ / watchOS 2.0+
- Xcode 8.0+
- 使用 Swift 3 构建
安装
CocoaPods
要将 SwiftLinkPreview 作为 pod 包使用,只需在您的 Podfile 文件中添加以下内容。
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
target 'Your Target Name' do
use_frameworks!
// ...
pod 'SwiftLinkPreview', '~> 3.2.0'
// ...
end
Carthage
要将 SwiftLinkPreview 作为 Carthage 模块包使用,只需在您的 Cartfile 文件中添加以下内容。
// ...
github "LeonardoCardoso/SwiftLinkPreview" ~> 3.2.0
// ...
Swift Package Manager
要将 SwiftLinkPreview 作为 Swift Package Manager 包使用,只需在您的 Package.swift 文件中添加以下内容。
import PackageDescription
let package = Package(
name: "Your Target Name",
dependencies: [
// ...
.Package(url: "https://github.com/LeonardoCardoso/SwiftLinkPreview.git", "3.2.0")
// ...
]
)
手动
您只需将所有内容(但不是 .plist 文件)放入 Sources
文件夹中,并将其拖入 Xcode 项目(确保启用“如果需要则复制项目”和“创建分组”)。
使用方法
初始化
import SwiftLinkPreview
// ...
let slp = SwiftLinkPreview(session: URLSession = URLSession.shared,
workQueue: DispatchQueue = SwiftLinkPreview.defaultWorkQueue,
responseQueue: DispatchQueue = DispatchQueue.main,
cache: Cache = DisabledCache.instance)
请求预览
let preview = slp.preview("Text containing URL",
onSuccess: { result in print("\(result)") },
onError: { error in print("\(error)")})
// preview.cancel() to cancel it.
结果 是一个 Response
结构体
Response {
let url: URL // URL
let finalUrl: URL // unshortened URL
let canonicalUrl: String // canonical URL
let title: String // title
let description: String // page description or relevant text
let images: [String] // array of URLs of the images
let image: String // main image
let icon: String // favicon
let video: String // video
let price: String // price
}
取消请求
let cancelablePreview = slp.preview(...,
onSuccess: ...,
onError: ...)
cancelablePreview.cancel()
启用并访问缓存
SLP 包含内置内存缓存,因此按照以下方式创建对象
let slp = SwiftLinkPreview(cache: InMemoryCache())
获取缓存响应
if let cached = self.slp.cache.slp_getCachedResponse(url: String) {
// Do whatever with the cached response
} else {
// Perform preview otherwise
slp.preview(...)
}
如果您想创建自己的缓存,只需实现此协议并在对象初始化器中使用它。
public protocol Cache {
func slp_getCachedResponse(url: String) -> SwiftLinkPreview.Response?
func slp_setCachedResponse(url: String, response: SwiftLinkPreview.Response?)
}
流程
重要
您需要在项目的 Info.plist 文件中将 Allow Arbitrary Loads
设置为 YES
。
如果您不想使用上面的选项并且使用 SwiftLinkPreview
提供您的服务,您也可以在 Info.plist 文件中将其列入白名单。您可以在此处了解更多信息这里、这里 和 这里。
提示
并非所有网站都会包含信息,你可以按需处理你的实现得到的信息。但这里提供两个发布预览的技巧:
- 如果某些信息缺失,你可以向用户提供输入的机会。比如描述信息。
- 如果检索到多个图像,你可以提供用户选择一个图像的功能。
测试
请随意fork此仓库,并在 SwiftLinkPreviewTests/URLs.swift 中添加URLs以测试URL并帮助改进此库。URL数量越多,可靠性越好。
信息和联系方式
由 @LeonardoCardoso 开发。
您可以通过Twitter @leocardz 或发送电子邮件至 [email protected] 与我联系。
相关项目
许可证
The MIT License (MIT)
Copyright (c) 2016 Leonardo Cardoso
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.