Can I open a link using the button and textfield on which the link is listed on another page within the app itself and not with Safari?
I'm using Swift 4
You have to open webview in destination view controller and send url string from first view controller to destination like below:
class ViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
#IBAction func action(_ sender: Any) {
if let mainVC = storyboard?.instantiateViewController(withIdentifier: "second") as? DestinationViewController {
mainVC.urlStr = textField.text
self.navigationController?.pushViewController(mainVC, animated: true)
}
}
}
Destination View controller:
class DestinationViewController: UIViewController {
var urlStr: String?
override func viewDidLoad() {
super.viewDidLoad()
let webView = UIWebView(frame: self.view.frame)
self.view.addSubview(webView)
if let urlString = urlStr, let url = URL(string: urlString) {
webView.loadRequest(URLRequest(url: url))
}
}
}
Just Use WKWebView to open the link on Button action using this
Import WebKit
import WebKit
In your FirstViewController pass the URL while pushing to
SecondViewController
FirstViewController
if let secondVC = (UIStoryboard.init(name: "Main", bundle: nil)).instantiateViewController(withIdentifier: "secondVCID") {
secondVC.url = textFiled.text // OR send the URL you want to send
self.navigationController?.pushViewController(secondVC, animated: true)
}
In SecondViewController
Import Webkit
declare the delegate
class SecondViewController: UIViewController,WKNavigationDelegate {
declare the URL var so that u cab access it n FirstVC
var url: String?
#IBOutlet weak var webView: WKWebView!
In ViewDidLoad Of SecondVC
let webUrl = NSURL(string: url!)
let request = NSURLRequest(URL: webUrl)
// load request in webview.
webView.navigationDelegate = self
webView.loadRequest(request)
and Implement all delegate methods of WKWebview
//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")
}
Related
First of all I'm new in swift. I have a WebView app for iOS device using swift language , can add button back to previous page ?
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://google.com/")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
let statusBarFrame = UIApplication.shared.statusBarFrame
let statusBarView = UIView(frame: statusBarFrame)
self.view.addSubview(statusBarView)
statusBarView.backgroundColor = .systemBackground
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
title = webView.title
}
}
I have a Xcode Project with a Webview and a TabBar and with the TabBar I can switch between WebViews. My Problem is that when I put something in my ShoppingCard under Lieferworld.de and switch with the TabBar to my Shopping Card url the Items in there are not Visible. How can I solve this? the ShoppingCard URL ends with .php. Below is the code which is implemented
I also uploaded a video on YouTube were I you can see the error
https://youtu.be/qU3Mu1G7MY0
Viewhome:
import UIKit
import WebKit
class viewHome: UIViewController, WKUIDelegate {
#IBOutlet var webViewHome: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webViewHome = WKWebView(frame: .zero, configuration: webConfiguration)
webViewHome.uiDelegate = self
webViewHome.configuration.preferences.javaScriptEnabled = true
//webViewHome.configuration.preferences.javaEnabled = true
view = webViewHome
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://lieferworld.de")
let request = URLRequest(url: url!)
webViewHome.configuration.preferences.javaScriptEnabled = true
//webViewHome.configuration.preferences.javaEnabled = true
webViewHome.load(request)
}
#IBAction func GoBackHome(_ sender: Any) {
if webViewHome.canGoBack {
webViewHome.goBack()
}
}
#IBAction func GoForwardHome(_ sender: Any) {
if webViewHome.canGoForward {
webViewHome.goForward()
}
}
}
ViewShopping | Shopping Cart Class:
import UIKit
import WebKit
class viewShopping: UIViewController, WKUIDelegate {
#IBOutlet var webViewShopping: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webViewShopping = WKWebView(frame: .zero, configuration: webConfiguration)
webViewShopping.uiDelegate = self
//webViewShopping.configuration.preferences.javaEnabled = true
webViewShopping.configuration.preferences.javaScriptEnabled = true
view = webViewShopping
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://lieferworld.de/warenkorb.php")
let request = URLRequest(url: url!)
webViewShopping.configuration.preferences.javaScriptEnabled = true
//webViewShopping.configuration.preferences.javaEnabled = true
webViewShopping.load(request)
}
#IBAction func goBackShoppingCart(_ sender: Any) {
if webViewShopping.canGoBack {
webViewShopping.goBack()
}
}
#IBAction func goForwardShoppingCart(_ sender: Any) {
if webViewShopping.canGoForward {
webViewShopping.goForward()
}
}
#IBAction func webViewRefresh(_ sender: Any) {
webViewShopping.reload()
}
}
WKNavigationDelegate has three delegates.
Here's an example;
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
print(error.localizedDescription)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("Start page load")
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
{
let title = webView.title
print("finish load. \(title).")
// you can do processing on the results here or trigger processing elsewhere
webView.evaluateJavaScript("document.documentElement.outerHTML.toString()",completionHandler:
{ (html: Any?, error: Error?) in
print(html as Any)
self.htmlSource = html as! String
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "pageLoaded"), object: nil)
})
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.urlText.stringValue = String(describing: webView.url!)
}
This does not work 100% of the time.
An example would be when I navigate to reddit. I click a thread and it changes URL which is fine, but if I click the reddit home button, it doesn't change URL to www.reddit.com even after it is 100% loaded.
Any help is appreciated.
What else is going on in your view controller? I'm not able to reproduce your problem with the code below. Have you tried this in an isolated project?
import Cocoa
import WebKit
class ViewController: NSViewController {
#IBOutlet weak var webView: WKWebView!
#IBOutlet weak var urlLabel: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
let req = URLRequest(url:
URL(string: "https://reddit.com")!)
webView.load(req)
}
#IBAction func getUrlClicked(_ sender: Any) {
guard let url = webView.url else { return }
urlLabel.stringValue = url.absoluteString
print("url: \(String(describing: webView.url!))")
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
How can i use safari-open in new tab in my webkit app
I am creating a internet browser and i need that
Just like the photo
For example, I want to show the open in new tab option when I hold my finger on a link in Google
enter image description here
class ViewController: UIViewController, UITextFieldDelegate, WKNavigationDelegate {
#IBOutlet weak var backButton: UIButton!
#IBOutlet weak var forwardButton: UIButton!
#IBOutlet weak var webView: WKWebView!
#IBOutlet weak var urlTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
urlTextField.delegate = self
webView.navigationDelegate = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear( animated )
let urlString:String = "https://www.google.com"
let url:URL = URL(string: urlString)!
let urlRequest:URLRequest = URLRequest(url: url)
webView.load(urlRequest)
urlTextField.text = urlString
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let urlString:String = urlTextField.text!
let url:URL = URL(string: urlString)!
let urlRequest:URLRequest = URLRequest(url: url)
webView.load(urlRequest)
textField.resignFirstResponder()
return true
}
#IBAction func backButtonTapped(_ sender: Any) {
if webView.canGoBack {
webView.goBack()
}
}
#IBAction func forwardButtonTapped(_ sender: Any) {
if webView.canGoForward {
webView.goForward()
}
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
backButton.isEnabled = webView.canGoBack
forwardButton.isEnabled = webView.canGoForward
urlTextField.text = webView.url?.absoluteString
}
}
Use this delegate method in bellow you last medhod :
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.targetFrame == nil {
if let url = navigationAction.request.url {
let app = UIApplication.shared
if app.canOpenURL(url) {
app.open(url, options: [:], completionHandler: nil)
}
}
}
decisionHandler(.allow)
}
is It oky ?
I've a ViewControllerWeb to show a WebView. I need nav bar to show some options to user, so I need that NavigationBar is showed.
Problem: The navigation bar is not showed.
ViewControllerWeb.swift
import UIKit
import WebKit
class ViewControllerWeb : UIViewController, WKNavigationDelegate {
#IBOutlet weak var webView: WKWebView!
#IBOutlet weak var progress: UIActivityIndicatorView!
var url : String?
override func viewDidLoad() {
self.webView.allowsBackForwardNavigationGestures = true
self.webView.navigationDelegate = self
let request = URLRequest(url: URL(string: url!)!)
self.navigationController?.navigationBar.isHidden = false
self.webView.load(request)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("didFinish")
self.progress.stopAnimating()
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print("didiFail")
self.progress.stopAnimating()
}
func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
print("webViewWebContentProcessDidTerminate")
self.progress.stopAnimating()
}
func webView(_ webView: WKWebView,
didStartProvisionalNavigation navigation: WKNavigation!) {
print("didStartProvisionalNavigation")
self.progress.startAnimating()
}
func webView(_ webView: WKWebView,
didFailProvisionalNavigation navigation: WKNavigation!,
withError error: Error) {
print("didFailProvisional")
self.progress.stopAnimating()
}
}
Storyboard
Result on simulator iPhone7
The NavigationBar isn't showed.
Present ViewControllerWeb with UINavigationController.
// Storyboard name
let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
// Storyboard withIdentifier
let viewController = storyBoard.instantiateViewController(withIdentifier: "ViewControllerWeb")
let navBarController = UINavigationController(rootViewController: viewController)
self.present(navBarController, animated: true, completion: nil)