简体   繁体   中英

My UIsearch bar does not work/send request to other sites

I'm still new to this but I'm confused on why my search bar does not work any time i put in a url address. I'm trying to make a video downloader app.

import UIKit
import AFNetworking
import Alamofire

class ViewController: UIViewController {

@IBOutlet var Webview: UIWebView!
@IBOutlet var SearchBar: UISearchBar!

override func viewDidLoad() {


    if let url = URL(string: "https://www.google.com"){

        let requestObj = URLRequest(url: url)

        Webview.loadRequest(requestObj)
    }
    SearchBar.text = "http://"

}

func searchBarSearchButtonClicked(searchbar: UISearchBar) {

    searchbar.resignFirstResponder()

    let text = SearchBar.text
    let url = NSURL(string: text!)
    let request:URLRequest = URLRequest(url: url! as URL)

    //let request = NSURLRequest(URL: url! as URL)

    Webview.loadRequest(request)

}

}

Add UISearchBarDelegate protocol methods to fire a search bar protocol method;

class ViewController: UIViewController, UISearchBarDelegate, UIWebViewDelegate {
    override func viewDidLoad() {
        SearchBar.delegate = self
        webview.delegate = self // connect your webview in storyboard
    }

    // MARK: -  UISearchView Delegates
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    }

    func searchBarSearchButtonClicked(searchbar: UISearchBar) {
        // call your function here
               let searchText = searchbar.text!
               let url = URL (string: searchText)
               let requestObj = URLRequest(url: url!)
               webView.loadRequest(requestObj)

        }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM