我有以下代码:
UIWebView.loadRequest(NSURLRequest(URL: NSURL(string: "google.ca")))
我收到以下错误:
“ NSURLRequest”不可转换为UIWebView。
知道是什么问题吗?
Answers:
loadRequest:是实例方法,不是类方法。您应该尝试使用UIWebview的实例作为接收器而不是类本身来调用此方法。
webviewInstance.loadRequest(NSURLRequest(URL: NSURL(string: "google.ca")!))
但是,正如@radex在下面正确指出的那样,您还可以利用currying来调用如下函数:
UIWebView.loadRequest(webviewInstance)(NSURLRequest(URL: NSURL(string: "google.ca")!))
迅捷5
webviewInstance.load(NSURLRequest(url: NSURL(string: "google.ca")! as URL) as URLRequest)
UIWebView.loadRequest(webviewInstance)(…)
webviewInstance.loadRequest(NSURLRequest(url: NSURL(string: "google.ca")! as URL) as URLRequest)
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.
}
//////////////////////////////////////////////////// ////////////////////如果要使用webkit
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.webView.frame.size.height))
self.view.addSubview(webView)
let url = URL(string: "your URL")
webView.load(URLRequest(url: url!))`
尝试这个:
将UIWebView添加到视图。
使用助手编辑器连接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.example.com")
let requestObj = URLRequest(url: url! as URL)
webView.load(requestObj)
}
并运行该应用程序!
@IBOutlet weak var myWebView: UIWebView!
将其重新连接,但错误仍在显示。
?
到myWebView?.loadRequest(requestObj);
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")!))
在Swift 4或4.2中,您可以这样使用:
您的看法就像下面这样:
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看起来像下面
将URL加载到WebView非常容易。只需在情节提要中创建一个WebView,然后就可以使用以下代码加载url。
let url = NSURL (string: "https://www.simplifiedios.net");
let request = NSURLRequest(URL: url!);
webView.loadRequest(request);
就这么简单,只有三行代码:)
参考:UIWebView示例
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/
简单,经过测试且可以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! as URL)
// init and load request in webview.
webView = WKWebView(frame: self.view.frame)
webView.navigationDelegate = self
webView.load(request as URLRequest)
self.view.addSubview(webView)
self.view.sendSubview(toBack: 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")
}
快速4.2、5+
@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 3.1及更高版本
let url = NSURL (string: "Your Url")
let requestObj = NSURLRequest(url: url as! URL);
YourWebViewName.loadRequest(requestObj as URLRequest)
Swift 4更新以编程方式创建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)
}
}