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!
Related
I am creating an app in which I am using ARSKView. I am seeing that it is not releasing memory on dismissing ViewController. I am not able to find any issue in my code.
Here is my code for using ARSKView.
class ARViewController: UIViewController, ARSKViewDelegate {
weak var sceneView: ARSKView?
override func viewDidLoad() {
super.viewDidLoad()
sceneView = ARSKView(frame: self.view.frame)
self.view.addSubview(sceneView!)
sceneView?.delegate = self
let scene = CustomScene(size: view.frame.size)
scene.sceneDelegate = self
sceneView?.presentScene(scene)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
activateARView()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
sceneView?.session.pause()
}
var configuration = ARWorldTrackingConfiguration()
func activateARView() {
configuration.worldAlignment = .gravityAndHeading
sceneView?.session.run(configuration)
}
}
The controller is just paused, you can try by setting the controller equal to "nil" just make sure all dependencies are cleared before that.
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()
}}
I am trying to wrap my website inside a WKWebView but an anchor with href="tel:312133" is not showing any action when clicked on it.
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let mUrl = URL(string: "https://www.example.com")
let mRequest = URLRequest(url: mUrl!)
webView.load(mRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I am actually using a WKWebView in my IOS application. The url loaded successfully but when clicking on the Facebook and Twitter share buttons nothing happens. How can I solve this issue?
Here is the following code:
ViewController:
import UIKit
import WebKit
class ViewController: UIViewController,WKNavigationDelegate {
#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()
let url = NSURL(string: "https://www.examplepage.com")!
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.
}
}
I have loaded a webpage to xcode WebView. But only the upper portion of the page is loaded. I can not scroll down to the bottom of the page. Same fact for pdf. Only upper 2 pages can be scrolled. What can i do ?Here is my code.
Thanks in advance.
import UIKit
class ViewController: UIViewController {
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
var URL = NSURL(string: "http://www.archetapp.com")
webView.loadRequest(NSURLRequest(URL: URL!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//for pdf
import UIKit
class ViewController: UIViewController {
#IBOutlet var webViews: UIWebView!
var path = ""
override func viewDidLoad() {
super.viewDidLoad()
path = NSBundle.mainBundle().pathForResource("ibook", ofType: "pdf")!
let url = NSURL.fileURLWithPath(path)
webViews.scalesPageToFit = true
webViews.scrollView.scrollEnabled = true
webViews.userInteractionEnabled = true
self.webViews.loadRequest(NSURLRequest(URL: url!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Try this!
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var webView : UIWebView
override func viewDidLoad() {
super.viewDidLoad()
//load initial
path = NSBundle.mainBundle().pathForResource("ibook", ofType: "pdf")!
let url = NSURL.fileURLWithPath(path)
var req = NSURLRequest(URL : url)
webView.delegate = self // <---
webView.loadRequest(req)
}
func webViewDidStartLoad(webView : UIWebView) {
//UIApplication.sharedApplication().networkActivityIndicatorVisible = true
println("webViewDidStartLoad")
}
func webViewDidFinishLoad(webView : UIWebView) {
//UIApplication.sharedApplication().networkActivityIndicatorVisible = false
webViews.scalesPageToFit = true
webViews.scrollView.scrollEnabled = true
webViews.userInteractionEnabled = true
println("webViewDidFinishLoad")
}
}
I had the same issue just today and at least in my case it was caused by having the "Scale Pages to Fit" option checked in the WebView properties. I'm assuming Carlos' reply regarding the zoom scale fixes it anyways but I didn't need the option enabled in the first place so that was my easy fix.