需求說(shuō)明
1、搜索結(jié)果支持分享,生成H5頁(yè)面展示。
2、分享出去的H5頁(yè)面可以打開(kāi)114啦的搜索界面。
需求地址
相關(guān)邏輯及代碼
在請(qǐng)求綜合搜索時(shí),會(huì)返回一個(gè)用于分享的URL。
該url有固定格式: http://114larc.com/p/search?q=1 ->第一個(gè)q= 后面的都是搜索的關(guān)鍵詞。
因?yàn)樗阉鳑](méi)有相關(guān)經(jīng)驗(yàn)時(shí)也會(huì)返回該字段,所以客戶端添加了判斷,
如果沒(méi)有相關(guān)內(nèi)容則不顯示分享搜索結(jié)果的選項(xiàng)。
if !json["data"]["share"].stringValue.isEmpty && tempDatas.count > 0
&& self.datas?.last?.type != .share {
let shareModel = SearchGroupModel(SearchGroupType.share)
shareModel.searchDatas = [SearchCellModel(keyword: json["data"]["share"].stringValue)]
self.datas?.append(shareModel)
}
當(dāng)點(diǎn)擊分享搜索結(jié)果時(shí)則自定義分享顯示的信息,不自定義的話會(huì)獲取當(dāng)前打開(kāi)的頁(yè)面相關(guān)的url和title。
case .share:
guard let model = groupModel.searchDatas?[indexPath.row] else { return }
PageViewManager.showShare(model.keyWord, shareText: "114啦 - 搜索\"\(String(describing: urlBarView!.addressTextField.text!))\"")
return
以上是分享出去的邏輯。
經(jīng)過(guò)確定,114啦中是不用打開(kāi)該分享出去的H5頁(yè)面的。
當(dāng)識(shí)別出http://114larc.com/p/search?q=(關(guān)鍵詞)鏈接時(shí),
或者第三方調(diào)用"oof.browser://openlink/?search/word=(關(guān)鍵詞)",
則使用原生UI打開(kāi)搜索界面并進(jìn)行搜索。
if url.absoluteString.hasPrefix("oof.browser://openlink/?search/word=") {//搜索用114啦打開(kāi)。。去除所有界面。打開(kāi)搜索界面。
let str = url.absoluteString.replacingOccurrences(of: "oof.browser://openlink/?search/word=", with: "")
if let strd = str.ud_UrlDecoded() {
if didWebFinishLoaded == true {
if let navC = window?.rootViewController as? UINavigationController {
let mainViewController = navC.viewControllers.first as! MainViewController
mainViewController.openSearchView(searchKey: strd)
}
}else{
openSearchKey = strd
}
}
return false
}
func openSearchView(searchKey: String) {
resetMainViewController(false)
removePicBrowser()
showSearchView()
let key = searchKey.removingPercentEncoding ?? searchKey
urlBarView?.addressTextField.text = key
searchViewManager.textFieldText = key
}
//需要將各種彈層移除。再新建標(biāo)簽頁(yè)打開(kāi)。。 分享彈層 菜單彈層 搜索界面。。
func resetMainViewController(_ hideSearch: Bool = true) {
var isViaUserCommon = true//默認(rèn)用戶直接點(diǎn)擊常用欄,直接跳轉(zhuǎn)到這個(gè)界面
if let _ = self.presentedViewController as? UINavigationController {
isViaUserCommon = false
}
if let _ = self.presentedViewController as? TabManageViewController {
self.dismiss(animated: true, completion: nil)
}
if isViaUserCommon {
self.navigationController?.popToRootViewController(animated: false)
} else {
self.navigationController?.dismiss(animated: false, completion: nil)
}
if let urlbarview = urlBarView , hideSearch == true {//收起搜索界面。
urlbarview.cancelBtnClicked(urlbarview.cancelButton)
}
}
移除蓋在mainview上的控制器
func removePicBrowser() {
var currentVc : UIViewController!
if let newNav = self.presentedViewController as? UINavigationController {
currentVc = newNav.viewControllers.last!
} else {
currentVc = self.navigationController?.viewControllers.last ?? self
}
for vc in currentVc.childViewControllers {
if let picVc = vc as? PicBrowser {
picVc.btnClicked(picVc.cancelBtn)
}
if let picVc = vc as? PictureBrowser {
picVc.btnClicked(picVc.cancelBtn)
}
}
if let window = UIApplication.shared.keyWindow {
for view in window.subviews {
if let menu = view as? YYWMenuView {
menu.disMiss()
}
if let action = view as? ActionView {
action.disMiss()
}
}
}
}
在PageView的loadRequest(_ request: URLRequest)中攔截搜索的url。使用原生UI打開(kāi)。
if strd.contains("114larc.com/p/search?q=") || strd.contains("114la.com/p/search?q=") {//搜索的界面
strd = strd.substring(from: strd.range(of: ".com/p/search?q=")!.upperBound)
mainViewController().openSearchView(searchKey: strd)
return
}
還有如果在搜索框輸入114la.com/p/search?q=(關(guān)鍵詞) 再點(diǎn)擊鍵盤(pán)的前往按鈕,
則將關(guān)鍵詞截取出來(lái)放在搜索框中進(jìn)行搜索。
期間會(huì)有對(duì)url的encode與decode操作,確保關(guān)鍵詞正確。