"Init(URL:)" has been renamed to "init(url:)" - swift 3 wkwebview error - swift

First time using webkit alongside swift 3, and I keep getting this error regarding a web view load request. Why is Xcode announcing the renaming but maintaining the error?
var webView: WKWebView!
var websites = ["apple.com", "hackingwithswift.com"]
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "https://" + websites[0])!
webView.load(NSURLRequest(URL: url as URL) as URLRequest)
webView.allowsBackForwardNavigationGestures = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

As suggested by #LeoDabus, here's the code
class Controller: UIViewController {
var webView: WKWebView!
var websites = ["apple.com", "hackingwithswift.com"]
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://" + websites[0])!
webView.load(URLRequest(url: url) as URLRequest)
webView.allowsBackForwardNavigationGestures = true
}
}

I had the same issue which i solved using following lines of code
let url = NSURL (string: "http://www.sourcefreeze.com")
let requestObj = NSURLRequest(url: url! as URL)
webview.loadRequest(requestObj as URLRequest)

Related

WKWebView - Margin Top

I'm starting swift.
Code is a webview without superior navigation, constrain moves the webview from the top leaving an upper margin.
see the image
Image Result
My code
import UIKit
import WebKit
class ViewController: UIViewController,WKNavigationDelegate, WKUIDelegate {
lazy var webView: WKWebView = {
let webConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
webView.translatesAutoresizingMaskIntoConstraints = false
return webView
}()
func setupUI() {
self.view.backgroundColor = .red
self.view.addSubview(webView)
NSLayoutConstraint.activate([
webView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
webView.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor),
webView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
webView.rightAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.rightAnchor)
])
}
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
let myURL = URL(string: "https://www.google.com.br/")
let myRequest = URLRequest(url: myURL!,cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy,timeoutInterval: 10.0)
webView.load(myRequest)
}
}
Solved. Just add:
self.navigationController? .isNavigationBarHidden = true

URL validation not working in swift

started learning swift two weeks ago, with no previous programming experience, and I can't for the life of me figure out why this wouldn't work to check for nil. it just crashes when trying to load a web page if the user enters an invalid URL. This is the ENTIRETY of the code.
import UIKit; import WebKit
class ViewController: UIViewController {
#IBOutlet weak var adressBar: UITextField!
#IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func returnPressed(_ sender: Any) {
if let adressBarText = adressBar.text {
if let myURL = URL(string: adressBarText) {
let myRequest = URLRequest(url: myURL)
webView.load(myRequest)
adressBar.resignFirstResponder()
print("EYYYYY")
} else {
print("BOOOO")
}
}
}
}
Try this method
func verifyUrl (urlString: String?) -> Bool {
//Check for nil
if let urlString = urlString {
// create NSURL instance
if let url = NSURL(string: urlString) {
// check if your application can open the NSURL instance
return UIApplication.sharedApplication().canOpenURL(url)
}
}
return false
}
https://stackoverflow.com/a/30130535/8069241

Swift3 UIWebView delegate method not being called

Code :
let termWebView = UIWebView(frame:CGRect(x:0, y:20, width:320, height:400))
termWebView.delegate = self
self.view.addSubview(termWebView)
if let url = URL(string: “http://www.google.co.in”)
termWebView.loadRequest(request)
}
func webView(_ webView: UIWebView, didFailLoadWithError error: Error)
{
}
func webViewDidFinishLoad(webView: UIWebView!)
{
}
I created a simple webview and set the delegate.but the delegate methods are not being called.what is wrong with code?any help will be appreciated.thanks in advance
Try this -
class TouchUIViewController: UIViewController, UIWebViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let termWebView = UIWebView(frame:CGRect(x:0, y:20, width:320, height:400))
termWebView.delegate = self
self.view.addSubview(termWebView)
let urlString = "http://www.google.co.in/"
if let url = URL(string: urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!) {
let request = URLRequest(url: url as URL)
termWebView.loadRequest(request)
}
}
func webViewDidStartLoad(_ webView: UIWebView) {
}
func webView(_ webView: UIWebView, didFailLoadWithError error: Error)
{
}
func webViewDidFinishLoad(_ webView: UIWebView)
{
}
}

Swift UIWebView remove Edge

As you can see in the Picture below there are edges.
When I embed the View with a NavigationView, than the edges appears. So how can I avoid this edges??
Thanks
import UIKit
class ShowSubmitViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var webView: UIWebView!
var URL : String = ""
var type : Int = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadBrowser()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadBrowser(){
switch type {
case 5:
let htmlString:String! = URL
webView.loadHTMLString(htmlString, baseURL: nil)
default:
let url = NSURL (string: URL);
let requestObj = NSURLRequest(URL: url!);
webView.loadRequest(requestObj);
}
}
}
Picture WebView with Edge

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

I get an error when I press a button on the sideBar stopping here:
webView.scalesPageToFit = true
webView.loadRequest(request)
I would like to ask help to solve the problem.
Below is my code of ViewController.swift
import UIKit
class ViewController: UIViewController, UISearchBarDelegate {
#IBOutlet weak var searchBar: UISearchBar!
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "http://www.apple.com")
let request = NSURLRequest(URL: url!)
webView.scalesPageToFit = true
webView.loadRequest(request)
}
func searchBarSearchButtonClicked(searchBar: UISearchBar! {
caricaUrl(searchBar.text)
}
func caricaUrl(url: String) {
let url = NSURL(string: "http://www.google.com/search?q=" + "\(url)")
let request = NSURLRequest(URL: url!)
webView.scalesPageToFit = true
webView.loadRequest(request)
}
func didReceiveMemoryWaarning() {
super.didReceiveMemoryWarning()
}
#IBAction func onBurger() {
(tabBarController as TabBarController).sidebar.showInViewController(self, animated: true)
}
}
Your web view is an optional value, so you either have to force unwrap it or say that it is an optional when calling it. Your app was trying to force unwrap it, and it came back with nil. That means you can't force unwrap the variable.
So you can change both of these lines in both places they are declared:
webView.scalesPageToFit = true
webView.loadRequest(request)
to these lines:
webView?.scalesPageToFit = true
webView?.loadRequest(request)
The question mark explicitly says that the webView variable is an optional.