I have two functions that i would like to call in another ViewController. I don't want to write the same code in the other Viewcontroller. I have tried creating a ViewController object and call the two methods this but keep getting crashes. I have also tried extending the UIViewController but no luck. How can i go on about
import UIKit
import WebKit
class PrivacyPolicyController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
super.loadView()
createWebView()
}
override func viewDidLoad() {
super.viewDidLoad()
loadURL(url: "https://www.apple.com")
}
func loadWebView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
func loadURL(url: String) {
guard let myURL = URL(string: url) else { return }
let myRequest = URLRequest(url: myURL)
webView.load(myRequest)
}
}
I wanna call loadURL and loadWebView functions in this ViewController
import UIKit
import WebKit
class TermsAndConditionsController: UIViewController{
override func loadView() {
super.loadView()
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
The ViewController where actions are performed
import UIKit
class WelcomeViewController: UIViewController {
#IBAction func termsAndConditionsTapped(_ sender: UIButton) {
let termsAndConditions = TermsAndConditionsController()
self.navigationController?.pushViewController(termsAndConditions, animated: true)
}
#IBAction func privacyPolicyTapped(_ sender: UIButton) {
let privacyPolicy = PrivacyPolicyController()
self.navigationController?.pushViewController(privacyPolicy, animated: true)
}
}
As I wrote in a comment, use the same viewcontroller for both the Privacy Policy and T&C:
import UIKit
import WebKit
class MyWebViewController: UIViewController {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
}
func loadWebView() {
let webConfiguration = WKWebViewConfiguration()
self.webView = WKWebView(frame: .zero, configuration: webConfiguration)
self.view.addSubview(self.webView)
webView.translatesAutoresizingMaskIntoConstraints = false
webView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
webView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
webView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
}
func loadURL(url: String) {
guard let myURL = URL(string: url) else { return }
let myRequest = URLRequest(url: myURL)
if (self.webView == nil) {
self.loadWebView()
}
self.webView.load(myRequest)
}
}
Then:
import UIKit
class MakeItHappenViewController: UIViewController {
#IBAction func termsAndConditionsTapped(_ sender: UIButton) {
let termsAndConditions = MyWebViewController()
self.navigationController?.pushViewController(termsAndConditions, animated: true)
termsAndConditions.loadURL("http://someurl.com")
}
#IBAction func privacyPolicyTapped(_ sender: UIButton) {
let privacyPolicy = MyWebViewController()
self.navigationController?.pushViewController(privacyPolicy, animated: true)
privacyPolicy.loadURL("http://someotherurl.com")
}
}
You can create Base class(Which inherits UIViewController) for that and make your both view controller inherit the base class. That way you can have both the methods in both classes and you can just make calls to them
Class BaseClass: UIViewController{
var webView: WKWebView
func loadWebView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
}
func loadURL(url: String) {
guard let myURL = URL(string: url) else { return }
let myRequest = URLRequest(url: myURL)
webView.load(myRequest)
}}
The base class now has all the required methods for creating adding and realoading url for web view. Now for view controller classes.
Class PrivacyPolicyController: BaseClass, WKUIDelegate {func methodWhereYouwantToCallWebviewMthods(){
self.loadWebView()
self.loadURL()
self.view = self.webView}}
Same for other class
class TermsAndConditionsController: BaseClass{func methodWhereYouwantToCallWebviewMthods(){
self.loadWebView()
self.loadURL()
self.view = self.webView}
override func loadView() {
super.loadView()
}
override func viewDidLoad() {
super.viewDidLoad()
}}
Related
So I have a view controller that sets a WKWebView as its view and it loads a PDF from a website. But the navigationBar cuts off the top of the PDF, but it doesn't cut it off when a website is loaded. Anyone know how to fix it?
class WebView: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
var urlString: String?
var pdf: NSData?
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.largeTitleDisplayMode = .never
let url = URL(string: urlString ?? "https://www.cpp.edu")!
webView.load(URLRequest(url: url))
pdf = NSData(contentsOf: url)
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))
}
#objc func shareTapped() {
let view = UIActivityViewController(activityItems: [pdf!], applicationActivities: nil)
present(view, animated: true)
}
}
I added a WKWebView to my main View, but for some reason when I run the app it gives me a whitescreen. Do I need to set some additional size variables to the view or webView variable?
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
view.frame = view.bounds
webView = WKWebView(frame: CGRect( x: 0, y: 20, width: 380, height: 150 ), configuration: webConfiguration)
webView.navigationDelegate = self
webView.uiDelegate = self
view.addSubview(webView)
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "https://www.google.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
}
loadView()
You should never call this method directly. The view controller calls
this method when its view property is requested but is currently nil.
This method loads or creates a view and assigns it to the view
property
You can try with viewDidLoad
import WebKit
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let webConfiguration = WKWebViewConfiguration()
view.frame = view.bounds
webView = WKWebView(frame: CGRect( x: 0, y: 20, width: UIScreen.main.bounds.width, height: 250 ), configuration: webConfiguration)
webView.navigationDelegate = self
webView.uiDelegate = self
view.addSubview(webView)
let myURL = URL(string: "https://www.google.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
}
The first line in documentation of UIViewController.loadView says:
Creates the view that the controller manages
Which means, there is no mainView in loadView, here you have to assign/load a view for the UIViewController.
So, you have to replace this line:
view.addSubview(webView)
With this line:
self.view = webView
Anyways, here is your fixed view controller that is using loadView:
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .null, configuration: webConfiguration)
webView.navigationDelegate = self
webView.uiDelegate = self
self.view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "https://www.google.com")!
let myRequest = URLRequest(url: myURL)
webView.load(myRequest)
}
}
Notice that I changed frame: .null, this too is managed by the ViewController.
Here is another class that shows you how to use addSubview instead of loadView to do the same
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.webView = WKWebView(frame: CGRect(x: 0, y: 20, width: 380, height: 150))
self.webView.navigationDelegate = self
self.webView.uiDelegate = self
self.view.addSubview(webView)
if let url = URL(string: "https://www.google.com") {
self.webView.load(URLRequest(url: url))
}
}
}
I have a log when I load youtube URL in WKWebView.
I have searched the same title of my question in stackOverFlow.
But it didn't work for me.
What's wrong with my code?
And I use swift4 & xcode9.2.
Thanks.
Transport also set true.
Warning like this:
Could not signal service com.apple.WebKit.Networking: 113: Could not find specified service
import UIKit
import WebKit
class DetailViewController: UIViewController {
var videoId: String = ""
var videoTitle: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
loadUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadUI() {
view.backgroundColor = UIColor.white
naviSetting()
webViewSetting()
}
func naviSetting() {
self.title = videoTitle
}
func webViewSetting() {
let webview = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
let url = URL(string: "https://www.youtube.com/watch?v=\(self.videoId)")
let request = URLRequest(url: url!)
webview.load(request)
self.view.addSubview(webview)
}
}
I have tried to encode parameter and Its working fine for me. Please check below code.
var wkWebView = WKWebView()
override func viewDidLoad()
{
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
self.wkWebView.translatesAutoresizingMaskIntoConstraints = false
self.wkWebView.frame = CGRect.init(x: 0, y: 0, width: self.wkWebView.frame.size.width, height: self.wkWebView.frame.size.height)
self.view.addSubview(wkWebView)
loadUrl()
}
override func viewWillLayoutSubviews()
{
super.viewWillLayoutSubviews()
self.wkWebView.frame = CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
self.wkWebView.contentMode = .scaleAspectFit
}
func loadUrl()
{
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true
let url_join = "v=1p38GWfCIhQ"
let myURL = URL(string: "https://www.youtube.com/watch?")
var myRequest = URLRequest(url: myURL!)
myRequest.httpMethod = "POST"
myRequest.httpBody = url_join.data(using: String.Encoding.utf8)
wkWebView.load(myRequest)
}
I hope It will also work for you. :)
For me this Code worked fine.
Notice, I Used http:// and not https://.
But you already have set the settings for http:// to work.
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet weak var webView: WKWebView!
private let url = URL(string: "http://www.google.com")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let request = URLRequest(url: url!)
webView = WKWebView(frame: self.view.frame)
self.view.addSubview(webView)
webView.navigationDelegate = self
webView.load(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I'm subclassing UIWebView but when I trying to make a request it crash with this error:
fatal error: unexpectedly found nil while unwrapping an Optional value
Here is my implementation:
ViewController:
var web:MyWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.webPage = MyWebView(frame: self.view.frame)
self.view.bringSubview(toFront: self.webPage!)
}
class MyWebView: UIWebView,UIWebViewDelegate {
let urlStr = "http://apple.com"
override init(frame: CGRect) {
super.init(frame: frame)
scalesPageToFit = true
scrollView.bounces = false
delegate = self
let urlRequest = URL(string: urlStr)
let request = URLRequest(url: urlRequest!)
self.loadRequest(request)
}
If check the value of urlRequest in the console:
po urlRequest
nil
Any of you knows what I'm doing wrong or why this urlRequest is set as nil?
I'll really appreciate your help.
This works for me fine
import UIKit
class MyWebView: UIWebView,UIWebViewDelegate {
let urlStr = "https://www.apple.com"
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
scalesPageToFit = true
scrollView.bounces = false
delegate = self
let urlRequest = URL(string: urlStr)
let request = URLRequest(url: urlRequest!)
self.loadRequest(request)
}
}
Then call it from a viewcontroller
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
let webView = MyWebView.init(frame: self.view.frame)
self.view.addSubview(webView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView : WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
self.view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "http://boooo.com")
webView.loadRequest(NSURLRequest(URL: url!))
webView.allowsBackForwardNavigationGestures = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Everything works fine but allowsBackForwardNavigationGestures = true not working in xcode 7.1. can't go back and forward in wkwebview.
Look like it's interfering with navigationController's interactivePopGestureRecognizer (if you have one).
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
}
So I just disabled this property at the time of using WKWebView in navigation stack!