2016-07-22 13 views
0

Ich schrieb diesen Code:Warum UIWebView meinen Link in Safari nicht öffnet?

if navigationType == UIWebViewNavigationType.LinkClicked { 
     UIApplication.sharedApplication().openURL(request.URL!) 
     return false 
    } 

und es funktioniert für alle normalen URLs. Aber wenn ich auf das Bild klicken/gif mit dem nächsten Code:

<a href="https://cdn-images-1.medium.com/max/800/1*IR7oti7mEsYunzp_HhcNig.gif" 
target="_blank" class="c" 
style="background-image: url('//cdn.iframe.ly/pj?url=https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1*IR7oti7mEsYunzp_HhcNig.gif&amp;width=1280&amp; 
key=9b0e65e32a5412cf736c430972d914c33f46250b&amp;cache=temp');"> 
1*IR7oti7mEsYunzp_HhcNig.gif </a> 

es öffnet dieses gif/URL in meiner Anwendung, innerhalb von UIWebView.

Wie kann ich erzwingen, dass es die Gif/URL auch in Safari öffnet?

+0

Sind Sie sicher, dass Link Ergebnisse in 'LinkClicked' Navigationstyp? Ich würde versuchen, den 'navigationType' - 'print (" \ (navigationType) ")' auszudrucken, nur um sicherzustellen, dass es der erwartete Wert ist. – dylansturg

+0

@DylanS es druckt 'Navigationstyp: UIWebViewNavigationType' =/ – Doe

+0

' print ("\ (navigationType == UIWebViewNavigationType.LinkClicked)") 'Es wird nur der Typ gedruckt, nicht der Wert. – dylansturg

Antwort

0

Etwas muss irgendwo anders in Ihrem Code gebrochen werden. Arbeiten fein wie folgt aus:

import UIKit 

class ViewController: UIViewController, UIWebViewDelegate { 

@IBOutlet var webView : UIWebView? 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    self.webView?.loadHTMLString("<a href='https://cdn-images-1.medium.com/max/800/1*IR7oti7mEsYunzp_HhcNig.gif' target='_blank' class='c' style='background-image: url('//cdn.iframe.ly/pj?url=https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1*IR7oti7mEsYunzp_HhcNig.gif&amp;width=1280&amp; key=9b0e65e32a5412cf736c430972d914c33f46250b&amp;cache=temp');'> 1*IR7oti7mEsYunzp_HhcNig.gif </a>", baseURL: NSURL(string: "testurl.com")) 
} 

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { 
     if navigationType == UIWebViewNavigationType.LinkClicked { 
      UIApplication.sharedApplication().openURL(request.URL!) 
      return false 

     } 

    return true 
} 
} 

Screenshot with the result

+0

Meine Navigationsart druckt 'Navigationstyp: UIWebViewNavigationType'. Ich denke, das ist ein Problem – Doe