0

I am using Swift and have web-based content (an external webpage) embedded in WebView for my native app. On that webpage, again, which is in-app, I need to make one link open in Safari and not in the App. HTML target _blank code on the webpage doesn’t work (I wish it was that easy), looking for the right code to do it in Swift.

I have used this code for uiwebview:

@IBOutlet var news: UIWebView!
   var theURL = "http://"

override func viewDidLoad() {
    super.viewDidLoad()
    loadWebPage()

}
func loadWebPage(){
    let requestURL = NSURL (string: theURL)
    let URLrequest = NSURLRequest (URL: requestURL!)
    news.loadRequest(URLrequest)

I have used this code for WKNaviagtionDelegate:

func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
    if (navigationAction.navigationType == WKNavigationType.LinkActivated && !navigationAction.request.URL!.host!.lowercaseString.hasPrefix("http://")) {
        UIApplication.sharedApplication().openURL(navigationAction.request.URL!)
        decisionHandler(WKNavigationActionPolicy.Cancel)
    } else {
       decisionHandler(WKNavigationActionPolicy.Allow)
    }

Best, Drew

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Drew Bennett
  • 483
  • 3
  • 6
  • 25

2 Answers2

0

Because you need to create a button that looks like a link.

that should do the trick:

UIApplication.sharedApplication().openURL(NSURL(string: "http://...")!)
xoum89
  • 79
  • 1
  • 1
  • 6
  • Thanks for your response, but I am already aware of that code. I got rejected by apple for not being user friendly enough. I am looking for a code which will cause the user to exit the uiwebview when they enter a certain url. – Drew Bennett Sep 11 '15 at 15:17
  • What version of IOS you want your app running? – xoum89 Sep 11 '15 at 15:26
  • I have my iOS version set for 7.0 and later and I am running xcode 6.4. – Drew Bennett Sep 11 '15 at 15:29
  • Can you check the policy off apple. Like 12.3 Apps that are simply web clippings, content aggregators, or a collection of links, may be rejected [link]https://developer.apple.com/app-store/review/guidelines/#functionality – xoum89 Sep 11 '15 at 15:35
  • Thanks for bringing this to my attention, but I have other parts of the app including emergency numbers, maps, and games. I am trying to make a part where students can easily navigate and understand the website. – Drew Bennett Sep 11 '15 at 15:39
0

If the button is actually on a webpage in WebView... the link is not really controlled by the App unless you manipulate the link in iOS. It looks like the HTML target="_new" tag on the button might work in a later version of iOS. It's a bug in iOS 7 and was fixed in 7.0.3. Try a higher iOS version target for the App with the HTML target tag on the button. How to open Safari from a WebApp in iOS 7

Community
  • 1
  • 1
CTorch
  • 1
  • 1