2

I Have a UIWebView strictly for viewing PDF's, and I don't want any link detection in the view, Like this:

enter image description here

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
            }
        }
    }
John Durand
  • 1,934
  • 5
  • 22
  • 34

3 Answers3

3

If your webview was added by the storyboard, You can disable this in the attributes inspector.

[Xcode Image1

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
douglazt
  • 142
  • 5
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;

}

Eluss
  • 512
  • 3
  • 5
0

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 :

enter image description here

Kirit Modi
  • 23,155
  • 15
  • 89
  • 112