Cocoa WebView to Open Links in Default Browser

Discussion in 'Mac Programming' started by nikiski, Apr 7, 2008.

  1. macrumors regular

    Joined:
    Oct 30, 2007
    Location:
    Hong Kong, or Surrey, BC, Canada
    #1
    Hi all,

    I'm wondering if there is a way to make a WebView to open a link with the system's default browser?

    Thanks.
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    You need to set the web view's policy delegate, and then implement the webView:decidePolicyForNavigationAction:request:frame:decisionListener: method, something like this:

    Code:
    - (void)webView:(WebView *)sender
            decidePolicyForNavigationAction:(NSDictionary *)actionInformation
            request:(NSURLRequest *)request
            frame:(WebFrame *)frame
            decisionListener:(id <WebPolicyDecisionListener>)listener
    {
        [listener ignore];
        [[NSWorkspace sharedWorkspace] openURL;[request URL]];
    }

    Edit: I can't get that code to format, but it should do what you need.
     

Share This Page