I Have a UIWebView strictly for viewing PDF's, and I don't want any link detection in the view, Like this:
So I added the following code to negate the detection of links and the presentation of that view:
override func viewDidLoad() {
super.viewDidLoad()
myWebView.dataDetectorTypes = UIDataDetectorTypes.None
}
However it still picks up links, and presents that modal view. How do I correctly implement code to stop this?
Here is my updated code:
class PdfViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var myWebView: UIWebView!
var contentUrlPassedOn: String!
var allowLoad = true
override func viewDidLoad() {
super.viewDidLoad()
myWebView.delegate = self
let url: NSURL! = NSURL(string: contentUrlPassedOn)
myWebView.loadRequest(NSURLRequest(URL: url))
myWebView.dataDetectorTypes = UIDataDetectorTypes.None
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
return allowLoad
}
func webViewDidStartLoad(webView: UIWebView) {
var hasFinishedLoading = false
updateProgress()
}
func webViewDidFinishLoad(webView: UIWebView) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC))),
dispatch_get_main_queue()) {
[weak self] in
if let _self = self {
_self.hasFinishedLoading = true
self!.allowLoad = false
}
}
}
If your webview was added by the storyboard, You can disable this in the attributes inspector.
[1
It's an objective-c code, but u can do the same in Swift. It will fix the interaction with links. The con is that they will be still detected.
- (BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if (inType == UIWebViewNavigationTypeLinkClicked) {
return NO;
}
return YES;
}
Finely Solve the problem for you,
Working Demo
Cannot disable long press link menu, Which is Bug in iOS, That's solve in iOS 8.2.
According to bug, when press long on link then its open Action Sheet, Now in iOS 8.2 its solve as below.
Load PDF using WKWebView as below.
import UIKit
import WebKit
class ViewController: UIViewController , WKUIDelegate {
var webView: WKWebView?
override func loadView() {
super.loadView()
self.webView = WKWebView()
self.view = self.webView
}
override func viewDidLoad() {
super.viewDidLoad()
super.viewDidLoad()
var url = NSURL(string:"http://www.nursing.umich.edu/studentresources/documentation/CreatingBookmarksPDFdocsV7.pdf")
var req = NSURLRequest(URL:url!)
self.webView!.loadRequest(req)
}
}
Now you Click on your long Press link then its doesn't open ActionSheet PopUp.
Enjoy..!!
Output :
Related
before posting this I was looking on the internet how to implement from cocoa pods MBCircularProgressBarView in iOS Swift WKWebKit to track progress of loading website.
I have tried with combination of some other progress bar codes but it didn't work.
I have tried to implement self.progressView.value = 0 in viewDidLoad
and
UIView.animate(withDuration: 1.0){
self.progressView.value = 100
}
in
didFinish navigation
which shows circular bar and animate but doesn't show proper loading progress.
Any idea how to make this work.
Make a class class WebpageViewController: UIViewController, UIWebViewDelegate containing:
#IBOutlet weak var activitySpinner: UIActivityIndicatorView!
#IBOutlet weak var activityLabel: UILabel!
override func viewDidAppear(_ animated: Bool)
{
activitySpinner.tintColor = R.color.YumaRed
activitySpinner.hidesWhenStopped = true
activityLabel.text = "Loading..."
webView.loadRequest(URLRequest(url: URL(string: "http://...")!))
}
func webViewDidStartLoad(_ webView: UIWebView)
{
activitySpinner.startAnimating()
}
func webViewDidFinishLoad(_ webView: UIWebView)
{
activitySpinner.stopAnimating()
activityLabel.isHidden = true
}
func webView(_ webView: UIWebView, didFailLoadWithError error: Error)
{
activitySpinner.stopAnimating()
}
override func viewDidLoad()
{
super.viewDidLoad()
webView.delegate = self
}
Why the webView is nil in iOS 8 but not in iOS 9?
and how should I use webview in iOS 8?
Here is the code (swift 2.2)
in a viewController class:
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.webView?.delegate = self // without '?' in iOS 8 crash
// in iOS 9 not crash
// debug code
if let webView = self.webView { // iOS 8 nil
print("\(webView)") // iOS 9 not nil
}
//...
}
Thanks!
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var webView: UIWebView!
let url = NSURL(string: "http://google.com")
override func viewDidLoad() {
super.viewDidLoad()
self.webView.delegate = self
let req = NSURLRequest(URL : url!)
self.webView.loadRequest(req)
}
func webViewDidStartLoad(webView: UIWebView) {
print("start loading")
}
func webViewDidFinishLoad(webView: UIWebView) {
print("finish loading")
}
//...
}
like this, this code can run perfectly in iOS8 & iOS9
One solution:
#IBOutlet weak var webViewXib: UIWebView!
var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
if let _ = self.webViewXib {
self.webView = self.webViewXib
} else {
self.webView = UIWebView()
self.webView.frame = self.view.bounds
self.view.addSubview(self.webView!)
}
self.webView!.delegate = self
// ...
}
The problem is the way how instantiate your View Controller, you are probably instantiating it as:
MyViewController *myViewController = [[MyViewController alloc] init]
With that you will not have the view ready in your viewDidLoad, instead you should try:
MyViewController *myViewController = [[MyViewController alloc] initWithNibName:"MyViewController" bundle:nil]
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.
I've made in Swift a webview & in that webview there are links to ics file on the same domain of the webview. Now, when you click this link in Safari, you can perfectly add this to your calendar, but when I try this in iOS simulator, or on my device, I can't 'open' the ICS file link.
Any idea what I am doing wrong? Thanks!
This is my code:
import UIKit
class FirstViewController: UIViewController {
#IBOutlet weak var webAgenda: UIWebView!
#IBOutlet weak var activityagenda: UIActivityIndicatorView!
var url = "http://******.be/agenda.php"
func loadURL () {
let requestURL = NSURL(string: url)
let request = NSURLRequest(URL: requestURL!)
webAgenda.loadRequest(request)
}
override func viewDidLoad() {
super.viewDidLoad()
loadURL()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidStartLoad(webView: UIWebView) {
activityagenda.hidden = false
activityagenda.startAnimating()
}
func webViewDidFinishLoad(webView: UIWebView) {
activityagenda.hidden = true
activityagenda.stopAnimating()
}
because you load a http site so in iOS9 will have problem with App Transport Security.
disable ATS here
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)
}
}