简体   繁体   中英

How to open url programmatically but not open in browser swift

I need open url programmatically when user click to cell , but not need segue to Safari browser . It is need for statistic on site.

This is need to imitation like post request

I do:

UIApplication.shared.openURL(URL(string: url)!)

But this open Safari browser .

If I'm not mistaking, you want to open the URL in the application it self (without navigating to external -Safari- browser). Well, if that's the case, you should use UIWebView :

You can use the UIWebView class to embed web content in your app. To do so, create a UIWebView object, attach it to a window, and send it a request to load web content. You can also use this class to move back and forward in the history of webpages, and you can even set some web content properties programmatically.

WebViewController:

I suggest to add a UIWebView into a ViewController (called WebViewController) and present the ViewController when needed; ViewController on storyboard should looks like:

在此处输入图片说明

DON'T forget to assign a Storyboard ID for it.

And the ViewController:

class WebViewController: UIViewController   {
    @IBOutlet weak private var webVIew: UIWebView!

    var urlString: String?

    override func viewDidLoad() {
        if let unwrappedUrlString = urlString {
            let urlRequest = URLRequest(url: URL(string: unwrappedUrlString)!)

            webVIew.loadRequest(urlRequest)
        }
    }

    @IBAction private func donePressed(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }
}

Usage:

Consider that you want present it when the user taps a button in another ViewController:

class ViewController: UIViewController {
    @IBAction private func donePressed(_ sender: Any) {
        let stoyrboard = UIStoryboard(name: "Main", bundle: nil)

        // withIdentifier: the used storyboard ID:
        let webViewController = stoyrboard.instantiateViewController(withIdentifier: "WebViewController") as! WebViewController
        webViewController.urlString = "https://www.google.com/"

    }
}

Or specifically for your case (selecting a cell):

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // withIdentifier: the used storyboard ID:
    let webViewController = stoyrboard.instantiateViewController(withIdentifier: "WebViewController") as! WebViewController
    webViewController.urlString = "URL GOES HERE..."
}

Hope this helped.

if your question really means that you have some data on an external website that you need to access, so that you can extract a piece of information from it, then you probably don't need to display the rendered webpage at all.

You can extract the html content of a page like this

    var text = ""

    let url = URL(string: "https://www.bbc.co.uk")
    do
    {
        text = try String(contentsOf: url!, encoding: String.Encoding.utf8)
    }
    catch
    {
        print("error \(error.localizedDescription)")
    }
    if !text.isEmpty
    {
        parseThisPageSomehow(text)
    }

How you parse the text depends on what you need to get out of it, but this approach will give you the data you need.

If you use http rather than https , you will run into the well-documented transport security issue, which will force you to either set up a temporary override for http, or start using https

Transport security has blocked a cleartext HTTP

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