I want the webkit view to display my domain webpage but it won't work. It works for regular known websites but not for my own domain. I am using the code below. Also I added this to the pList file but no joy. Thanks in advance.
pList
NSAppTransportSecurity
NSAllowsArbitraryLoads
Code:
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView()
webView.navigationDelegate = self
view = webView
webView.frame = view.bounds
webView.navigationDelegate = self
let url = URL(string: "https://www.MYDOMAIN.co.uk")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
}
}
Try for this
class YourCalssviewController : UIViewController ,WKNavigationDelegate ,WKUIDelegate{ }
Write in viewDidLoad()
let url = NSURL(string: "https://www.MYDOMAIN.co.uk")
let request = NSURLRequest(url: url! as URL)
self.webView.load(request as URLRequest)
your code is working fine, the problem here with the URL itself, if you tried an other URL like "https://www.youtube.com/" it will work fine
Related
I'm a beginner in programming and am trying to make a simple app for displaying a webpage after choosing from menu options. However, I'm having trouble linking the code window to the storyboard, especially for linking a new page for the app to a new class. I think it's partly b/c I can't view the code and storyboard side by side.
I've also written code for displaying the website in on an app page, but I'm getting an error message saying "Argument passed to call that takes no arguments" (code below). Any tips on how I can address these?
import WebKit
import UIKit
//class ViewController: UIViewController {
class WebViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad() {
let url = URL(string: "https://google.com")!
webView.load(URLRequest(url: url))
}
}
override func viewDidLoad() {
super.viewDidLoad() {
let url = URL(string: "https://google.com")!
webView.load(URLRequest(url: url))
}
}
Above should be:
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://google.com")!
webView.load(URLRequest(url: url))
}
without the inner brackets.
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView : WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.google.com")!
webView.load(url)
webView.allowsBackForwardNavigationGestures = true
}
}
Hello, I am new to Swift programming. I am trying to load a simple website on navigation controller with the help of WKWebView. But I am unable to continue with it. As I am facing an error with line
let URL = let url = URL(string: "https://www.google.com")!
I need to know two things
What does the error mean?
How can I resolve it?
The error is pretty clear. You need to pass a URLRequest object as parameter.
let url = URL(string: "https://www.google.com")!
webView.load(URLRequest(url: url))
I wanted to load the excel sheet url/pdf url (document url) in WKWebView. As I am trying to open that url from browser then it directly downloads that document. So I am confused whether 1] I have to directly load the url into WKWebView or 2] first download those files and then load that local path in WKWebView.
If I go with the 2nd option then will my document download every time?
Following is the code which I have tried but it shows nothing
import UIKit
import WebKit
class OpenReportsFullScreenVC: UIViewController,WKUIDelegate {
var webView : WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
//first way :- webView.load(URLRequest(url: URL(string: "http://ipaddress/projects/ourivf/newourivf/assets/uploads/patient_image/attorney_details - Copy.xls")!))
// second way
let filePathURLData = "http://ipaddress/projects/ourivf/newourivf/assets/uploads/patient_image/attorney_details - Copy.xls"
let fileURL = URL(fileURLWithPath: filePathURLData )
webView.loadFileURL(fileURL, allowingReadAccessTo: fileURL)
}
override func loadView() {
//initialise webview
let webViewConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webViewConfiguration)
webView.uiDelegate = self
view = webView
}
}
Try with this code
import UIKit
import WebKit
class ViewController: UIViewController , **WKNavigationDelegate**{
var webView : WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let myBlog = "your URl"
let url = NSURL(string: myBlog)!
let request = NSURLRequest(url: url as URL)
webView = WKWebView(frame: self.view.frame)
webView.navigationDelegate = self
webView.load(request as URLRequest)
self.view.addSubview(webView)
self.view.sendSubview(toBack: webView)
}
}
File path cannot have blank characters like in your example.
I just started developing with swift, so I am sorry if the question is basic/stupid.
I have the following setup, just a test
import WebKit
import UIKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://hackingswift.com")!
webView.load(URLRequest(url:url))
webView.allowsBackForwardNavigationGestures = true
}
}
Unfortunately the browser doesn't load. The simulator only shows an empty navigation bar.
Suggestions? I am following a tutorial on hackingswift, so it's supposed to work.
You have to add webView as a subview or make an IBOutlet using Interface builder.
Try this:
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView?
func loadView() {
webView = WKWebView()
webView?.navigationDelegate = self
self.view.addSubview(webView!)
}
override func viewDidLoad() {
super.viewDidLoad()
self.loadView()
let url = URL(string: "https://hackingswift.com")!
webView?.load(URLRequest(url:url))
webView?.allowsBackForwardNavigationGestures = true
}
}
If you want it a bit more simple (without nullable variable), for example:
class ViewController: UIViewController, WKNavigationDelegate {
var webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
self.view.addSubview(webView)
webView?.allowsBackForwardNavigationGestures = true
self.loadUrl("https://hackingswift.com")
}
func loadUrl(_ url: String) {
if let url = URL(string: url) {
webView.load(URLRequest(url:url))
}
}
}
EDIT: it looks like some websites to load, while others do not, even if they are secure. If I put apple.com in the example, it loads, but a few others do not
Your url should be started with http or https for the webView to load.
Another possible reason is that your url containing an invalid certificate. Add the delegate function below into your code. You have to let WKWebView to bypass the certificate checking. However, this code is never recommended to go into production. You should be careful about what website your webView should and will load.
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: #escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
completionHandler(.useCredential, cred)
}
The problem is this line:
let url = URL(string: "https://hackingswift.com")!
There is no such URL on the Internet, so you're not actually going to see anything. (You won't see anything if you paste that URL into any browser.)
So change that line to this:
let url = URL(string: "https://www.hackingwithswift.com")!
Now run the app, and presto, you'll see the web site:
apologies for the simple question:
I am following Apple's guide on WKWebView to the dot. Same code and everything.
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "https://amerifit.afvusa.com/MenuMain.aspx?a=NTgzMjEyMTU3")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}}
Source
This is the URL I am trying to link to: https://amerifit.afvusa.com/MenuMain.aspx?a=NTgzMjEyMTU3
Except that I am getting a white screen when the viewcontroller tries to load the page.
Any reasons why? The code works fine for Google's homepage and this URL is secure so that isn't the issue.
Also, the website is not mine and I cannot edit it's files.