gpt4 book ai didi

ios - 搜索栏和网页 View

转载 作者:行者123 更新时间:2023-11-30 12:44:43 25 4
gpt4 key购买 nike

我正在尝试构建一个简单的网络浏览器。网络浏览器本身工作得很好,但是当我尝试在搜索栏中搜索网址时,我无法返回键盘来打开网站。我该如何解决这个问题?

@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var webView: UIWebView!

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
let text = searchBar.text
let urlSB = NSURL(string: text!)
let reqSB = NSURLRequest(url:urlSB! as URL)
self.webView!.loadRequest(reqSB as URLRequest)
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let url = NSURL(string: "https://www.google.com")
let req = NSURLRequest(url:url! as URL)
self.webView!.loadRequest(req as URLRequest)

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

最佳答案

我为你写了一个演示:

结果:

第 1 步:

enter image description here

第 2 步:输入您要访问的网址。

enter image description here

第3步:结果,webView加载您在searchBar中输入的url:

enter image description here

代码如下:

import UIKit

class ViewController: UIViewController, UISearchBarDelegate {

@IBOutlet weak var searchBar: UISearchBar!

@IBOutlet weak var webView: UIWebView!

override func viewDidLoad() {
super.viewDidLoad()

let url = NSURL(string: "https://www.baidu.com")
let req = NSURLRequest(url:url! as URL)
self.webView!.loadRequest(req as URLRequest)

searchBar.delegate = self

}

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
let text = searchBar.text
let urlSB = NSURL(string: text!)
let reqSB = NSURLRequest(url:urlSB! as URL)
self.webView!.loadRequest(reqSB as URLRequest)
}

// MARK: - searchbar delegate
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

let urlStr = "https://" + searchBar.text!

let url = NSURL(string: urlStr)
let req = NSURLRequest(url:url! as URL)
self.webView!.loadRequest(req as URLRequest)

}

如果你的webView没有加载url,并报告:

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

您可以像下面这样进入info.plist打开ATS,或者搜索SO找到详细方法:

enter image description here

关于ios - 搜索栏和网页 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41778012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com