PDF from WKWebView gets cut off by navigation bar? - ios

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)
}
}

Related

iOS webView is not using whole screen

I try to show a full responsive bootstrap website in a webView app. The app should just load my website as if I load the website in the browser of the smartphone/tablet.
However, the app looks like this on tablets:
How can I make the app full responsive?
Whole Code:
ViewController.swift
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// 1 The webView loads the url using an URLRequest object.
let url = URL(string: "https://www.blizz-z.de/")!
webView.load(URLRequest(url: url))
// 2 A refresh item is added to the toolbar which will refresh the current webpage.
let refresh = UIBarButtonItem(
barButtonSystemItem: .refresh,
target: webView,
action: #selector(webView.reload)
)
toolbarItems = [refresh]
navigationController?.isToolbarHidden = true
navigationController?.isNavigationBarHidden = true
}
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
let color = UIColor.black;
self.view.backgroundColor = color
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
title = webView.title
}
}
There is no problem in your code.
Switch to a target settings in Xcode, on a tab "General" in a section "Deployment target" switch "Devices" to "Universal".
Also add "Launch Screen.storyboard" if you did not add it.
you should add constraint for the webView, and the constant of top/bottom/left/right constraint is 0 to superView, not safe area
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.setupWebview()
let refresh = UIBarButtonItem(
barButtonSystemItem: .refresh,
target: webView,
action: #selector(webView.reload)
)
toolbarItems = [refresh]
navigationController?.isToolbarHidden = true
navigationController?.isNavigationBarHidden = true
}
func setupWebview(){
self.webView.frame = UIScreen.main.bounds
self.webView.navigationDelegate = self
self.view.backgroundColor = UIColor.black
let url = URL(string: "https://www.blizz-z.de/")!
self.webView.load(URLRequest(url: url))
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
title = webView.title
}
}
Try this... (Note: Not a copy pasted code)

WKWebView loaded twice, trying to go back I have to press twice on back navigation button

I'm trying to show a website using the WKWebView, it works fine as for the internet work.
problem is it is loading the site twice (it seems like it's going to another ViewController).
here is my code:
#IBOutlet weak var myWebView: WKWebView!
override func loadView() {
myWebView = WKWebView()
myWebView.navigationDelegate = self as? WKNavigationDelegate
view = myWebView
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.isHidden = false
let url = URL(string: "https://google.com")!
myWebView.load(URLRequest(url: url))
}

Calling a function from one ViewController to another

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()
}}

open url in webview when button is pressed

So I've got a button which I'm trying to use as a means for the user to navigate to a specific URL (which it does), however the button is opening the url in safari, and i want it to be presented in my web view.
Here is my view controller.swift file:
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var myProgressView: UIProgressView!
#IBOutlet weak var Home_button: UIButton!
var theBool: Bool = false
#IBOutlet weak var refresh_button: UIButton!
#IBAction func go_home(_ sender: UIButton) {
UIApplication.shared.openURL(NSURL(string:"my url")! as URL)
}
var myTimer = Timer()
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
myTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(webViewDidStartLoad(_:)), userInfo: nil, repeats: true)
let websiteurl = URL(string: "another url")
let websiteurlrequest = URLRequest(url: websiteurl!, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 10.0)
webView.loadRequest(websiteurlrequest)
}
func webViewDidStartLoad(_ webView: UIWebView) {
//if let myProgressView.progress is not nil{
myProgressView?.progress += 0.045
//}
}
func webViewDidFinishLoad(_ webView: UIWebView) {
let loadinglabel = self.view.viewWithTag(100)
loadinglabel?.removeFromSuperview()
myProgressView?.progress = 100
myProgressView?.removeFromSuperview()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func go() {
myProgressView?.progress += 0.005
}
}
How do I open the url in the web view on the button press, rather than in safari?
Thanks in advance!
Try this. Should work in Swift 3
#IBAction func go_home(_ sender: UIButton) {
yourWebView.loadRequest(URLRequest(url: URL(string: "http://www.google.com")!))
}
Change your button action method like this:
#IBAction func go_home(_ sender: UIButton) {
webView.loadRequest(websiteurlrequest)
}
and remove webView.loadRequest(websiteurlrequest) method from viewDidLoad. Then your webView load content when click on button.
Add the following code to the button action area
self.openWebURL(url: webUrl)
Function to load URL
func openWebURL(url : URL)
{
let safariVC = SFSafariViewController(url: url)
present(safariVC, animated: true, completion: nil)
}
SFSafariViewControllerDelegate should be added to class header like thise
class YourClassName: UIViewController, SFSafariViewControllerDelegate
Hope this helps...

How to: watchkit button displays image at url in Swift?

I want a button press in Watchkit to display an image from a URL. Given there's no UIWebView in Watchkit, my assumption is that I will need to trigger via the watch button, the loading of a UIWebView for the URL on the phone, store in an UIImage, that I can pass back to the watch to display. First is that the best strategy, secondly can anyone help me with the code for storing a UIImage from UIWebView in Swift?
Phone's ViewController:
import UIKit
class ViewController: UIViewController {
var URLPath = "myURL.jpeg"
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
loadAddressURL()
}
//to fit webpage within webView
override func viewDidAppear(animated: Bool) {
webView.frame = UIScreen.mainScreen().bounds
webView.center = self.view.center
webView.scalesPageToFit = true
}
func loadAddressURL () {
let url = NSURL(string: URLPath)
let requestObj = NSURLRequest(URL: url!)
webView.loadRequest(requestObj)
}
}

Resources