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

我想从我的iOS应用程序中将字符串传递给Google搜索,以便我可以在Safari中获取结果。

你可以这样做:

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) 

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

如果您使用自己的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) }