Reputation: 45
I have a web page link that will not display via iOS WKWebView
. How do I get links like this to display via WKWebView
?
It appears that only 3 version of https://www.google.com and http://www.apple.com will display a Web page using http:// or https://. How do I get the other web pages to load via WKWebView? (note: all of the web sites load inside of Google Chrome and Apple Safari without using the http:// syntax.)
Below is the code from ViewDidLoad:
@IBOutlet weak var myWebkitView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
`if let myUrl = URL(string:"https://www.ihs-us.org")`
{
let myURLRequest = URLRequest(url: myUrl)
myWebkitView.load(myURLRequest)
}
}
Below links works only for Apple and Google.
https://www.google.com will shows a Web site
http://www.apple.com also will shows its web site
schools.nyc.gov/SchoolPortals/08/X282 -- causes a crash
ihs-us.org // causes a crash, and does not show a web page
Upvotes: 0
Views: 435
Reputation: 478
All browsers append http:// to the url(if not present) and usually if a web page supports SSL, there will be a re-direction to https:// site. I developed a browser for internal use and any URL user enters, I append http:// to the URL. The simplest way would be
class func getValidURLFromString(_ urlString: String)->String{
if !urlString.hasPrefix("http://") && !urlString.hasPrefix("https://"){
return "http://" + urlString
}
else {
return urlString
}
}
Edited: ViewController Code
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let urlString = isValidURLFormat("schools.nyc.gov/SchoolPortals/08/X282")
let myURL = URL(string: urlString)
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
func isValidURLFormat(_ urlString: String) -> String {
if !urlString.hasPrefix("https://") && !urlString.hasPrefix("http://"){
return "http://" + urlString
}
return urlString }
}
Plist Setting
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true/>
</dict>
Upvotes: 1
Reputation: 2425
Try this -
import UIKit
import WebKit
class ViewController: UIViewController{
@IBOutlet var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let urlString = "ihs-us.org"
if urlString.hasPrefix("https://") || urlString.hasPrefix("http://"){
let myURL = URL(string: urlString)
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}else {
let correctedURL = "http://\(urlString)"
let myURL = URL(string: correctedURL)
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
}
}
For schools.nyc.gov/SchoolPortals/08/X282 -
And for ihs-us.org -
You just need to do the following steps-
Open your Info.plist
file of your project. Right click on it and add a row.
And after that just add those two lines in your (as it is) -
Upvotes: 0