By RoylesPika


2018-08-10 10:28:45 8 Comments

Website won't load in the WebView (UIWebView ) but xCode doesn't show an error. When I change the Website adress into google.com or what ever, it loads without problems. But the website I want to load is an usual url-website an works perfectly at normal browser and more. Can somebody maybe help me?

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var myViewController: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let url = URL(string: "can'tShowYouTheWebsite")
        myViewController.loadRequest(URLRequest(url: url!))
    }

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

3 comments

@Pedro Cavaleiro 2018-08-10 15:43:00

I remember having a similar problem Open your Info.plist

Add a new entry (by tapping the plus icon in the highlighted row) enter image description here

Then type "App Transport Security Settings" without the quote marks and after adding that key add an subkey "Allow Arbitrary Loads" and set it to yes. Your Info.plist should look partially like this

enter image description here

All this assuming that you're trying to load a properly assembled URL

(protocol://server/path)

@That_some_guy 2018-08-10 12:44:41

My advise is to use WKWebView instead of UIWebView and without an @IBOutlet

1- Declare a WKWebView.

var myNewView = WKWebView()

2- create a function like addConstrainsToWebView() and add it to override func loadView to add some constraints (adapt the code to your needs)

addConstrainsToWebView() {
            let web = WKWebView()

            self.view.addSubview(web)
            web.translatesAutoresizingMaskIntoConstraints = false
            NSLayoutConstraint.activate([
                web.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 40),
                web.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -40),
                web .bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -40),
                web .topAnchor.constraint(equalTo: view.topAnchor, constant: 40),
                web.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
                web.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
                ])
}

3- assign the view to your webView declared before

self.myNewView = web

4- create a function to create your web view (adapt the code to your needs)

func createWebView(withURL: String) {

    // Change UIView background color and opacity
    myNewView.isOpaque = false
    myNewView.backgroundColor = UIColor.clear
    myNewView.scrollView.backgroundColor = UIColor.clear
    myNewView.layer.backgroundColor = UIColor.clear.cgColor

    // enable / disable user interaction
    myNewView.isUserInteractionEnabled = false

    // Add rounded corners to UIView
    myNewView.layer.cornerRadius = 25

    // Add border to UIView
    myNewView.layer.borderWidth = 0.5

    // Change UIView Border Color to Red
    myNewView.layer.borderColor = some UIColor...

    let myURL = URL(string: withURL)
    let myURLRequest:URLRequest = URLRequest(url: myURL!)

    myNewView.load(myURLRequest)

    // Add UIView as a Subview
    self.view.addSubview(myNewView)
}

5- call this function in override func loadView and pass your string URL

6- your override func loadView method should be like this :

 override func loadView() {
        super.loadView()
            addConstrainsToWebView()
            createWebView(withURL ("your_URLString"!)
}

Hope this helps.

@Muhammad Bilal Hussain 2018-08-10 12:10:50

first of all call webview delegate

webView.delegate = self

Then assign your URL to the load request method

self.webView.loadRequest(URLRequest(url: URL(string: "URL")!))

It will definitely work for you.

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] Expand and Collapse tableview cells

8 Answered Questions

[SOLVED] How to detect tableView cell touched or clicked in swift

0 Answered Questions

UIScrollView cannot display image either in Portrait or Landscape mode

  • 2015-06-03 10:13:43
  • MilkBottle
  • 202 View
  • 0 Score
  • 0 Answer
  • Tags:   ios swift

1 Answered Questions

[SOLVED] TableView not displaying text with JSON data from API call

0 Answered Questions

How to plot y axis in swift using charts

  • 2017-12-17 05:26:58
  • Prez
  • 60 View
  • 0 Score
  • 0 Answer
  • Tags:   swift charts

1 Answered Questions

Web view: Returning nil at loadRequest while true in URLRequest

1 Answered Questions

[SOLVED] unable to add SSRadioButtonsController radio button title from code

  • 2016-02-11 14:23:12
  • Sahil
  • 411 View
  • 0 Score
  • 1 Answer
  • Tags:   ios swift

1 Answered Questions

[SOLVED] Swift: (Pre-)load UIWebView from initialViewController

3 Answered Questions

Swift 2 iOS 9 animation disappears after button text changed

  • 2015-08-10 09:11:34
  • nepete
  • 1033 View
  • 1 Score
  • 3 Answer
  • Tags:   ios swift2

1 Answered Questions

Sponsored Content