Skip to content
TheChatArchiveThe Chat Archive
o3-mini· OpenAIHugging Face LMArena Human Preference 140K

I have a Swift ios app with a webview. In that webview, I'm trying to use an anchor link…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Qwenqwen3-235b-a22b-no-thinking’s answer.
  1. User

    I have a Swift ios app with a webview. In that webview, I'm trying to use an anchor link to link to content lower down in the HTML page. When I tap on that link in the webview, it leaves the webview and takes me back to the home screen of my app. What's happening and how do I fix it?

  2. Assistant

    This behavior usually happens when the WebView’s navigation‐delegate is intercepting what should be an in–page “scroll” (an anchor jump) and incorrectly handling it as if it were an external link. In other words, when you tap an anchor link (for example, one with an href="#section2" or even sometimes with target="_blank"), the delegate method (such as webView(_:decidePolicyFor:decisionHandler:) for WKWebView or webView(_:shouldStartLoadWith:navigationType:) for UIWebView) is being called. If your code isn’t checking for “same page” navigation (or a nil target frame), it may end up handling that navigation action by opening a new view or otherwise “leaving” the current WebView (or even popping the view controller).

    Here are two common causes and solutions:


    1. Anchor Links Treated as External Requests

    What’s happening:
    When you tap an anchor link (e.g. <a href="#content">), the URL doesn’t change its host—it only appends a fragment. However, if your delegate method isn’t checking for this case and, for example, insists on opening every link externally or by some custom logic, it might cause your app to “pop” or navigate away from the WebView.

    How to fix it:
    Update your navigation delegate method to detect when the requested URL is simply a change in fragment. For example, in a WKWebView you might do something like this:

    func webView(_ webView: WKWebView,
                 decidePolicyFor navigationAction: WKNavigationAction,
                 decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    
        if let url = navigationAction.request.url,
           let currentURL = webView.url {
    
            // Check if the new URL is the same as the current URL except for the fragment.
            let currentComponents = URLComponents(url: currentURL, resolvingAgainstBaseURL: false)
            let newComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
            
            if currentComponents?.scheme == newComponents?.scheme &&
               currentComponents?.host == newComponents?.host &&
               currentComponents?.path == newComponents?.path {
                // It’s just an anchor jump; allow it.
                decisionHandler(.allow)
                return
            }
        }
        
        // Otherwise, handle your navigation logic here.
        // For example, if you normally open external links outside the WebView:
        if navigationAction.navigationType == .linkActivated {
            // Custom handling, such as:
            // UIApplication.shared.open(url)
            
            // And then cancel the WebView navigation.
            decisionHandler(.cancel)
            return
        }
        
        decisionHandler(.allow)
    }
    

    This way, if the new URL is merely a fragment change on the same page, you let the WebView handle it (which will simply scroll to the anchor).


    2. target="_blank" and Nil Target Frames

    What’s happening:
    Sometimes, HTML links will include target="_blank". In WKWebView, this causes the navigation action’s targetFrame to be nil because the link is trying to open in a new window. Many developers, in an effort not to open Safari, detect a nil target frame and then load the request in the current WebView—but if that check isn’t implemented correctly (or if it’s hooked up to custom navigation logic) it might end up “navigating away” from your intended view.

    How to fix it:
    Implement the navigation delegate method that detects a nil target frame and forces the request to load in your current WebView:

    func webView(_ webView: WKWebView,
                 createWebViewWith configuration: WKWebViewConfiguration,
                 for navigationAction: WKNavigationAction,
                 windowFeatures: WKWindowFeatures) -> WKWebView? {
    
        // If targetFrame is nil, it indicates a target="_blank" link.
        if navigationAction.targetFrame == nil,
           let url = navigationAction.request.url {
            webView.load(URLRequest(url: url))
        }
        return nil
    }
    

    This effectively “catches” the new-window request and loads it in the same WebView.


    Summary

    • What’s happening: Your tapped anchor link (via a fragment or with target="_blank") is being intercepted by your delegate methods. Since your code isn’t checking for same–page navigation (or nil target frames), it ends up handling the request in a way that pushes you out of your WebView (e.g. by opening an external URL or performing a custom navigation that returns you to the home screen).

    • How to fix it:

      • In your delegate’s decidePolicyFor (or similar) method, check if the requested URL is just the same page with a different fragment. If so, allow it.
      • Also, if you support target="_blank", implement the createWebViewWith delegate method to load such requests in the current WebView.

    By adding these checks, your anchor links will simply scroll to the correct part of the page without leaving your WebView.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.