我尝试使用启动webview的按钮制作应用程序。
我已经关注了许多有关该主题的教程和阅读不同主题但我无法使其正常工作:我在测试代码时收到此消息:
"Cannot call value of non-function type UIWebView!"
这是我到目前为止所做的步骤
这是WebViewController类(我在运行项目时遇到错误)
import UIKit
class WebViewController: UIViewController{
@IBOutlet weak var myWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//define url
let url = NSURL (string: "http://www.my-url.com")
//request
let req = NSURLRequest(url: url as! URL)
//load request into the webview
myWebview(req as URLRequest) //error happens here :
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
谢谢!
答案 0 :(得分:0)
尝试使用:
let url = NSURL (string: "https://google.com")
let request = NSURLRequest(url: url as! URL)
self. myWebView.loadRequest(request as URLRequest)
此代码适用于我
答案 1 :(得分:0)
您可以使用SFSafariViewController:
import SafariServices
let url = URL(string: "https://www.google.com")
let safariVC: SFSafariViewController = SFSafariViewController(url: url)
self.present(safariVC, animated: true, completion: nil)
我使用了swift 3语法。 该代码打开了Safari Web视图,您无需在故事板中创建segues和view controlles。