UIWebView centring page and allow navigation - swift

I managed to have a website loaded in my UIWebView but I can't manage to center it.
I also would like to be able to "navigate" into the website. What am I doing wrong ?
here's the code :
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var WebView: UIWebView!
var URLPath = "http://google.fr"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
WebView.scalesPageToFit = true
WebView.keyboardDisplayRequiresUserAction = true
loadAdressURL()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadAdressURL () {
let requestURL = NSURL(string:URLPath)
let request = NSURLRequest(URL: requestURL)
WebView.loadRequest(request)
}
}
And here's the IOS Simulator :
Rob.

Add this
override func viewDidAppear(animated: Bool) {
WebView.frame = UIScreen.mainScreen().bounds
WebView.center = self.view.center
}

Related

webViewDidStartLoad and webViewDidFinishLoad not working

I am trying to check if the web view is loading and if its finish loading and when I try to search on it this is what I have gotten however it seems like it's not working is it because I have done something wrong or they change the code for it? I did set the delegate for the web view so I don't think the issue is with the delegate.
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var activity: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://stackoverflow.com")
let urlRequest = URLRequest(url: url!)
webView.loadRequest(urlRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidStartLoad(_ webView: UIWebView){
print("Webview started Loading")
}
func webViewDidFinishLoad(_ webView: UIWebView) {
print("Webview did finish load")
}
You need to conform your class to UIWebViewDelegate and delegate managing of webView to your class:
class YourClass: UIViewController, UIWebViewDelegate {
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var activity: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://stackoverflow.com")
//!!!!!!!
webView.delegate = self
//!!!!!!!
let urlRequest = URLRequest(url: url!)
webView.loadRequest(urlRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidStartLoad(_ webView: UIWebView){
print("Webview started Loading")
}
func webViewDidFinishLoad(_ webView: UIWebView) {
print("Webview did finish load")
}
}
You need to add "delegate" in your UIWebView
webView.delegate = self

Add a top padding to IoS app WKWebView

I am trying to create a simple browser but the browser overlaps with operator and battery info icons on the top. I want to add 20px padding or margin on top of the app.
This is the only code I have in the viewController.swift
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet var containerView : UIView! = nil
var webView: WKWebView?
override func loadView() {
super.loadView()
self.webView = WKWebView()
self.view = self.webView!
}
override func viewDidLoad() {
super.viewDidLoad()
var url = NSURL(string:"http://nyt.com")
var req = NSURLRequest(URL:url!)
self.webView!.loadRequest(req);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
} }

UIWebView third party website with custom stylesheet

How can I add custom local stylesheet to UIWebView which is requested a third party website such as facebook.com.
import UIKit
class ViewController: UIViewController {
#IBOutlet var WebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let URL = NSURL(string: "www.any3rdpartywebsite.com" )
WebView.loadRequest(NSURLRequest(URL: URL!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

How to put override func before viewDidLoad()?

Open ViewController.swift for editing, and before viewDidLoad() put the code below the question into the default code of ViewController.Swift?
How do I put these 4 lines of code below using the directions above into the default code for ViewController.Swift?
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
Default code ViewController.Swift:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
I'm guessing you have a storyboard in which ViewController is defined. If so, you must not override loadView():
If you use Interface Builder to create your views and initialize the view controller, you must not override this method.
(from UIViewController class reference)
Instead edit the class of the view in the storyboard and set it to WKWebView. To get the webView reference, create it as an outlet:
#IBOutlet weak var webView: WKWebView!
And link it as a referencing outlet to the view in the storyboard.
(Or you could just put the WKWebView inside the default UIView; would be simpler. You could do this either programmatically in viewDidLoad() or via the storyboard.)
Or if you don't have a storyboard, then just copypaste the code into the editor inside the class but outside the existing functions?
import UIKit
class ViewController: UIViewController {
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
However, it matters not where the func goes in the class:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
}

Swift Web Browser

I am extremely new to swift, and have no experience with objective-C, only partial C++.
I am trying to create a simple web browser with swift, this is the following code I have:
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet var containerView: UIView!
var webView: WKWebView?
override func loadView() {
super.loadView()
self.webView = WKWebView()
self.view = self.webView
}
override func viewDidLoad() {
super.viewDidLoad()
var url = NSURL(string: "https://www.duckduckgo.com/")
var req = NSURLRequest(URL: url!)
self.webView!.loadRequest(req)
}
#IBAction func Searchresult(sender: AnyObject) {
url = NSURL(string: "https://www.duckduckgo.com/?q=\(sender)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Supposedly XCode says Use of unresolved identifier URL with #IBAction func Searchresult.
Once again I am greatly sorry if there is an obvious fix, I am extremely tired and not good at swift.
at first sight seems that you didn't declare url inside #IBAction, just put let url = NSURL(string: "https://www.duckduckgo.com/?q=\(sender)")