search google if an unqualified url is entered

This commit is contained in:
Jacob Bullock 2018-04-29 17:26:21 -10:00
parent 27077f1f8c
commit 267fbdd6c0

View file

@ -179,8 +179,16 @@ class WebBrowserViewController: UIViewController, UIWebViewDelegate, UIAlertView
}
@objc func loadAddress(_ sender: Any, event: UIEvent) {
guard let urlString = addressField?.text, var url = URL(string: urlString) else {
log.debug("Attempting to load invalid URL: \(addressField?.text ?? "nil")")
if let urlString = addressField?.text {
loadUrlString(urlString)
}
}
func loadUrlString(_ urlString: String, allowSearchRedirect: Bool = true) {
guard var url = URL(string: urlString) else {
if allowSearchRedirect {
loadSearchString(urlString)
}
return
}
if url.scheme == nil {
@ -189,6 +197,12 @@ class WebBrowserViewController: UIViewController, UIWebViewDelegate, UIAlertView
let request = URLRequest(url: url)
webView.loadRequest(request)
}
func loadSearchString(_ searchString: String) {
if let query = searchString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
loadUrlString("google.com/search?q=\(query)", allowSearchRedirect: false)
}
}
func updateAddress(_ request: URLRequest) {
let url: URL? = request.mainDocumentURL