Thunderhead ONE 搜索结果
版本 1.2.0
ThunderheadOneSearchResults是一个简单的模块,用于显示Thunderhead ONE的顶级谷歌搜索结果。它利用谷歌的定制搜索JSON API获取Thunderhead ONE的顶级谷歌搜索结果。
要求
- iOS 12.0+
- Swift 5.0
Cocoapods
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '12.0'
use_frameworks!
target '<Your Target Name>' do
pod 'ThunderheadOneSearchResults', '1.2.0'
end
入门
入门的最简单方法是将SearchResultsView附加到您的UIViewController视图属性。
您可以通过以下两种方式完成此操作。
- 通过界面构建器,将一个UIView(类型为SearchResultsView)添加到您的UIViewController视图属性中。
import UIKit
import ThunderheadOneSearchResults
class SearchResultsController: UIViewController {
@IBOutlet weak private var searchResultsView: SearchResultsView!
override func viewDidLoad() {
super.viewDidLoad()
searchResultsView.configure(
viewModel: SearchResultsViewModel(
searchServices: GoogleSearchServices(key: "AIzaSyCwDCCzK1a9ePx34ayA15Q8WKuiomJ58IE", searchEngineId: "014048833371632418532:eldqtbklbnq"),
amount: 10))
}
}
将UIView添加到您的UIViewController并将类型设置为SearchResultsView,如下所示:
- 通过编程实例化SearchResultsView并将其作为子视图添加到您的UIViewController视图属性中。
import UIKit
import ThunderheadOneSearchResults
class ProgrammaticSearchResultsController: UIViewController {
private var searchResultsView: SearchResultsView!
override func viewDidLoad() {
super.viewDidLoad()
searchResultsView = SearchResultsView(frame: view.bounds)
view.addSubview(searchResultsView)
searchResultsView.configure(
viewModel: SearchResultsViewModel(
searchServices: GoogleSearchServices(key: "AIzaSyCwDCCzK1a9ePx34ayA15Q8WKuiomJ58IE", searchEngineId: "014048833371632418532:eldqtbklbnq"),
amount: 10))
}
}
接下来,您需要通过调用SearchResultsView上的configure来为SearchResultsView提供一个SearchResultsViewModel。
SearchResultsViewModel需要一个遵循SearchServicesProtocol的服务。
您将使用的SearchResultsViewModel服务是GoogleSearchServices。为了使用GoogleSearchServices,您需要首先创建一个自定义搜索引擎并获取搜索引擎ID和访问其JSON API的谷歌密钥。key和searchEngineId都是初始化GoogleSearchServices所必需的。
请参考此处创建自定义搜索引擎和密钥的Google Custom Search JSON API:https://developers.google.com/custom-search/v1/overview。
GoogleSearchServices将通过“Thunderhead ONE”查询谷歌的JSON API,并将最相关搜索结果提供给SearchResultsView。
为了测试,您可以免费使用示例项目中相同的关键字“key”和“searchEngineId”。您也可以使用本框架提供的一项名为MockGoogleSearchServices的服务,该服务将返回一些模拟的SearchResult对象。