如何将文本从 textField 传递到 Google 从 iOS 应用搜索?

How to pass text from a textField to Google Search from an iOS app?

我想从我的 iOS 应用程序向 Google 搜索传递一个字符串,以便我能够在 Safari 中获取结果。

如果您使用自己的 Web 视图(您在评论 中指定,然后 将其更改为完全不同的内容),您可以使用 WKWebViewURLRequest加载并显示数据。 不要忘记转义查询字符串,例如:

@IBOutlet weak var webView: WKWebView!

func startSearch() {

    var textToSearch = "the answer to everything"
    // if there are spaces or other special characters,
    // you'll have to escape them:
    let allowedCharacters = NSCharacterSet.urlFragmentAllowed

    guard let  encodedSearchString  = textToSearch.addingPercentEncoding(withAllowedCharacters: allowedCharacters)  else { return }

    let queryString = "https://www.google.de/search?q=\(encodedSearchString)"
    guard let queryURL = URL(string: queryString) else { return }

    let myRequest = URLRequest(url:queryURL)
        webView.load(myRequest)
}

你可以这样做:

var query = "hello world"
query = query.replacingOccurrences(of: " ", with: "+")
var url = "https://www.google.co.in/search?q=" + query
UIApplication.shared.open(URL(string: url)!, options: [:], completionHandler: nil)

用您的搜索字符串替换查询。