如何在Swift中的UIWebView中加载URL?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Swift中的UIWebView中加载URL?相关的知识,希望对你有一定的参考价值。
我有以下代码:
UIWebView.loadRequest(NSURLRequest(URL: NSURL(string: "google.ca")))
我收到以下错误:
'NSURLRequest'无法转换为UIWebView。
知道问题是什么吗?
loadRequest:是一个实例方法,而不是类方法。您应该尝试使用UIWebview实例作为接收器而不是类本身来调用此方法。
webviewInstance.loadRequest(NSURLRequest(URL: NSURL(string: "google.ca")!))
但是,正如@radex在下面正确指出的那样,你也可以利用currying来调用这个函数:
UIWebView.loadRequest(webviewInstance)(NSURLRequest(URL: NSURL(string: "google.ca")!))
在Swift语言中使用Webview
let url = URL(string: "http://example.com")
webview.loadRequest(URLRequest(url: url!))
对于Swift 3.1及以上版本
let url = NSURL (string: "Your Url")
let requestObj = NSURLRequest(url: url as! URL);
YourWebViewName.loadRequest(requestObj as URLRequest)
容易,经过测试和工作100%
导入webkit:
import WebKit
将IBOutlet分配给webview:
var webView : WKWebView!
设置委托:
class ViewController: UIViewController , WKNavigationDelegate{
在viewDidLoad()上编写代码:
// loading URL :
let myBlog = "https://stackoverflow.com/users/4600136/mr-javed-multani?tab=profile"
let url = NSURL(string: myBlog)
let request = NSURLRequest(URL: url!)
// init and load request in webview.
webView = WKWebView(frame: self.view.frame)
webView.navigationDelegate = self
webView.loadRequest(request)
self.view.addSubview(webView)
self.view.sendSubviewToBack(webView)
编写委托方法:
//MARK:- WKNavigationDelegate
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
print(error.localizedDescription)
}
func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("Strat to load")
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
print("finish to load")
}
对于swift 4.2
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
//Func Gose Here
let url = URL(string: link)
let requestObj = URLRequest(url: url! as URL)
webView.loadRequest(requestObj)
}
在Swift 4或4.2中你可以使用:
- 添加WKWebView并连接到您的视图控制器。
- 你的观点如下:
import UIKit import WebKit class ViewController: UIViewController { @IBOutlet weak var wkwebview: WKWebView! override func viewDidLoad() { super.viewDidLoad() let request = URLRequest(url: URL(string: "**your URL**")!) wkwebview?.load(request) } }
- 允许任意加载true info.plist
<key>NSAppTransportSecurity</key> <dict> <key>Allow Arbitrary Loads</key> <true/> </dict>
注意info.plist看起来像下面
Swift 4 Update以编程方式创建WebView。
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://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.loadRequest(myRequest)
}}
import UIKit
class ViewController: UIViewController{
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
//Func Gose Here
loadUrl()
}
func loadUrl(){
let url = URL(string: "Url which you want to load(www.google.com)")
let requestObj = URLRequest(url: url! as URL)
webView.load(requestObj)
}
}
试试这个
let url = NSURL (string: "http://www.apple.com");
myWebView.loadRequest(NSURLRequest(URL: url!));
Swift中的UIWebView
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL (string: "url here")
let requestObj = URLRequest(url: url!)
webView.loadRequest(requestObj)
// Do any additional setup after loading the view.
}
试试这个:
- 将UIWebView添加到View。
- 使用助理编辑器连接UIWebview插座并命名您的“webview”。
- UIWebView加载URL。
@IBOutlet weak var webView: UIWebView! override func viewDidLoad() { super.viewDidLoad() // Your webView code goes here let url = URL(string: "https://www.google.co.in") let requestObj = URLRequest(url: url! as URL) webView.load(requestObj) }
并运行应用程序!
Swift 3在URL和URLRequest上不再使用NS前缀,因此更新的代码将是:
let url = URL(string: "your_url_here")
yourWebView.loadRequest(URLRequest(url: url!))
在Swift 3中你可以这样做:
@IBOutlet weak var webview: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Web view load
let url = URL (string: "http://www.example.com/")
let request = URLRequest(url: url!)
webview.loadRequest(request)
}
请记住在Info.plist
添加权限,添加以下值:
Swift 3 - Xcode 8.1
@IBOutlet weak var myWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL (string: "https://ir.linkedin.com/in/razipour1993")
let requestObj = URLRequest(url: url!)
myWebView.loadRequest(requestObj)
}
斯威夫特3
@IBOutlet weak var webview: UIWebView!
webview.loadRequest(URLRequest(url: URL(string: "https://www.yourvideo.com")!))
UIWebView loadRequest:使用NSURL对象创建NSURLRequest,然后将请求传递给uiwebview
,它会将请求的URL加载到Web视图中。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = NSURL (string: "http://www.google.com");
let requestObj = NSURLRequest(URL: url!);
myWebView.loadRequest(requestObj);
self.view.addSubview(myWebView)
}
更多参考:
http://sourcefreeze.com/uiwebview-example-using-swift-in-ios/
将URL加载到WebView非常简单。只需在故事板中创建一个WebView,然后就可
以上是关于如何在Swift中的UIWebView中加载URL?的主要内容,如果未能解决你的问题,请参考以下文章
Swift 2 - 无法在单独的 ViewController 中加载 UIWebView