How to load URL in UIWebView in Swift?
loadRequest: is an instance method, not a class method. You should be attempting to call this method with an instance of UIWebview as the receiver, not the class itself.
webviewInstance.loadRequest(NSURLRequest(URL: NSURL(string: "google.ca")!))
However, as @radex correctly points out below, you can also take advantage of currying to call the function like this:
UIWebView.loadRequest(webviewInstance)(NSURLRequest(URL: NSURL(string: "google.ca")!))
Swift 5
webviewInstance.load(NSURLRequest(url: NSURL(string: "google.ca")! as URL) as URLRequest)
How to load URL on WKWebView?
First of all you need to import
import WebKit
Following code is enough to open URL with WKWebView
let webView = WKWebView(frame: <#AnyRect#>)
let link = URL(string:"https://developer.apple.com/videos/play/wwdc2019/239/")!
let request = URLRequest(url: link)
webView.load(request)
Swift 4 WebKit webView does not load URL with added parameter
Your id is working fine:
override func viewDidLoad() {
super.viewDidLoad()
let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
self.view.addSubview(webView)
let url = URL(string: "https://www.youtube.com/watch?v=695PN9xaEhs")
webView.load(URLRequest(url: url!))
}
How can I get the URL from webView in swift
You are not getting url because webView
has not finished the loading of that requested URL
, you can get that URL
in webViewDidFinishLoad
method of UIWebviewDelegate
. For that you need to set delegate of webView
with your current ViewController
and need to implement UIWebviewDelegate
.
webView.delegate = self
Now you can get current loaded URL
of webView
in webViewDidFinishLoad
method.
func webViewDidFinishLoad(_ webView: UIWebView) {
if let text = webView.request?.url?.absoluteString{
print(text)
}
}
How to load a url link that is inside a web view and keep it in that web view in SWIFT
In the webview delegate you are going to want to navigate to the new url using that same webview.
For you this means that inside your switch case you should use
loadRequest
loadRequest documentation
To fix your issue you should remove the line using UIApplication.sharedApplication().openURL(request.URL!)
and replace it with a call to loadRequest
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
switch navigationType {
case .LinkClicked:
// Open links in Safari
guard let newRequest = request as? URLRequest else { return false }
webView.loadRequest(newRequest)
return false
default:
// Handle other navigation types...
return true
}
}
Hope this helps! Have a nice day!
UIWebView not loading a new url
Problem in
while(webView.isLoading) {
sender.isEnabled = false
}
You have frozen main thread and webView:decidePolicyForNavigationAction:request:frame:decisionListener: can't be invoke.
Try to use this:
func viewDidLoad()
{
...
webView.delegate = self;//UIWebView
//webView. navigationDelegate = self;//WKWebView
}
@IBAction func changeUrl(sender: UIButton) {
if LEDON {
let urlStr = NSString(format: "%@/?Led=0", self.url)
let req = URLRequest(url: URL(string: urlStr as String)!)
webView.loadRequest(req)
sender.isEnabled = false
LEDON = false;
} else {
let urlStr = NSString(format: "%@/?Led=1", self.url)
let req = URLRequest(url: URL(string: urlStr as String)!)
webView.loadRequest(req)
sender.isEnabled = false
LEDON = true;
}
}
For UIWebView you need to use
func webViewDidFinishLoad(UIWebView)
{
yourButton.isEnabled = true
}
For WKWebView
func webView(_ webView: WKWebView,
didFinish navigation: WKNavigation!)
{
yourButton.isEnabled = true
}
======================================
If you want use while loop try to change it for this:
while(webView.isLoading) {
sender.isEnabled = false
RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.5))
}
Swift UIWebView not loading http sites
This is due to the App Transport Security, a new protocol introduced by Apple at WWDC 2015. It doesn't allow any connections that are not HTTPS
. You can disable it, however it's not recommended as it secures your app.
To disable it you have to edit the App's .plist
. Simply right-click the .plist
file and select Open As -> Source File and add the following code:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>
This will allow HTTP
requests.
Hope that helps, Julian.
Related Topics
String Value to Unsafepointer≪Uint8≫ Function Parameter Behavior
Xcode 8 Beta 3 Use Legacy Swift Issue
Getting a "This Application Is Modifying the Autolayout Engine from a Background Thread" Error
Is Key-Value Observation (Kvo) Available in Swift
How to Run a Terminal Command in a Swift Script? (E.G. Xcodebuild)
Swiftui Ios14 - Navigationview + List - Won't Fill Space
Alamofire Get API Request Not Working as Expected
Deinit Method Is Never Called - Swift Playground
Swift - How to Convert String to Double
Do Swift-Based Applications Work on Os X 10.9/Ios 7 and Lower
Load a Uiview from Nib in Swift
How to Print the Type or Class of a Variable in Swift
How to Get Ip Address in Swift
Uitableview With More Than One Custom Cells With Swift