2

I have a WebView with a PDF file and a document in it contains hyperlinks that I want to disable. I tried using this approach but it didn't work, the links still open and load the nasty URLS:

  1. I put UIWebViewDelegate in my ViewController.h
  2. I then put this code in my ViewController.m:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (webView == myReadingArticlesWebView) {
        return NO;
    }
    else {
        return YES;
    }
}

Any ideas how to make this simple and easy to work? I admit that I could make some mistakes in the process I described above.

EDIT:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([request.URL isFileReferenceURL]) {
        return YES;
    } else {
        return NO;
    }
}

The code above does nothing for me as well

Sergey Grischyov
  • 11,995
  • 20
  • 81
  • 120
  • just out of curiosity, have you tried simply returning NO from that routine rather than putting inside the if-else logic? Does it stop moving out of the PDF to the link at that point? – trumpetlicks Jun 04 '12 at 20:52
  • Here is a StackOverflow linke you may wish to look at. Im not sure what you truly wish to do with your PDF, but if you want to have internally linked items stay active while dis-allowing externally linked items, this is where you should probable look. http://stackoverflow.com/questions/3889634/fast-and-lean-pdf-viewer-for-iphone-ipad-ios-tips-and-hints – trumpetlicks Jun 04 '12 at 21:04
  • @trumpetlicks of course, I did that and it didn't work – Sergey Grischyov Jun 04 '12 at 21:21
  • 1
    I believe this is because Once the PDF reader portion of Apples code takes over inside of the UIWebview, links are dealt with completely differnetly (i.e. they dont go through the standard UIWebView framework per-say). only direct webish type links can be controlled via shouldStartLoadWithRequest overload. take a look at the second comment I left and try to persue that idea to better control the entirety of you links within you PDF! – trumpetlicks Jun 04 '12 at 21:26
  • No Im just curious, have you tried putting an NSLog output in that routine without the if then to see if when tapping a link within your PDF if it is even getting into that delegate routine? This may also be an ignorant question, but are you also sure that your WebView is setup as having your controller be its delegate? – trumpetlicks Jun 04 '12 at 21:49
  • Well, I double-checked that and my delegate was not set in IB, but now, after it was set it doesn't even load the PDF file it should load on default. – Sergey Grischyov Jun 04 '12 at 21:57

3 Answers3

3

Just set the UIWebView's dataDetectorTypes property to None:

[myReadingArticlesWebView setDataDetectorTypes:UIDataDetectorTypeNone];
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • As commented above, I dont believe that the UIWebView continues control of links once the PDF viewer portion of apple's code takes over. If it cant be controlled with is overloading of shouldStartLoadWithRequest, then I dont think this method will work either. By the way, I really like this idea, and didnt know about it before, so I may find some good uses for it. THNX :-) – trumpetlicks Jun 04 '12 at 21:27
  • Doesn't matter if the document in the UIWebView is a PDF, HTML or whatever. overriding `setDataDetectorTypes:` should disregard the links even in a PDF document. – WrightsCS Jun 04 '12 at 21:29
  • I dont disagree, I think that it should. Unfortunately not in front of my mac to be able to try it though! – trumpetlicks Jun 04 '12 at 21:32
  • @SergiusGee refresh my answer. Also, place that statement in `viewDidLoad`, it should work for you. – WrightsCS Jun 04 '12 at 21:37
  • @WrightsCS Tried this out, I even put it in the `viewWillLoad` and still - nothing. Links still open. – Sergey Grischyov Jun 04 '12 at 21:48
  • I think that the problem may be in the links - they are not "http://" in the text, they look like this one: [link](http://example.com) – Sergey Grischyov Jun 04 '12 at 22:00
  • Well not really, you want to call it before you load any document. Yes, you can call it anywhere, but if you call it after you load a document, it's more than likely not ignore links. – WrightsCS Jun 04 '12 at 22:01
  • Doesn't matter what the text is, it searched the source of the document. – WrightsCS Jun 04 '12 at 22:01
1

Based on all the comments back and forth!!!

So now what you need to do is rather than simply returning NO in your

shouldStartLoadWithRequest 

method, you need to answer YES if the URL being loaded comes from local, and NO if it is coming from anywhere else. Use the

[request.URL isFileReferenceURL] 

method to check if it is a local file. NOTE: this method apparently only works on iOS 5 and later, look at

Check if NSURL is Local File

Hopefully this does it for you :-)

Community
  • 1
  • 1
trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
  • It's strange but it still doesn't work. I think I'll just go and have some sleep. It's already 2:15 AM here in Russia. Thanks for your effort, though! – Sergey Grischyov Jun 04 '12 at 22:15
0

If as trumpetlicks says the PDF viewer ignores what you say from shouldStartLoadWithRequest, and if you don't want to disable ALL links (which setting the datadetectors would do).

Then something else you could try is creating a class derived from NSURLProtocol and registering it in your app delegate. This would be able to intercept the network traffic originating from the pdf and give you the chance to filter links and stop ones you don't want going out.

Gruntcakes
  • 37,738
  • 44
  • 184
  • 378