Good afternoon, I am writing my first application on Swift.

There is a WebView where I open the site:

let defaultURL = "https://yandex.ru" override func viewDidLoad() { self.EasyW.frameLoadDelegate = self self.EasyW.policyDelegate = self self.EasyW.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: defaultURL)!)) } 

Everything works well, but when you click on an external link, nothing happens (How can I open an external link in the default browser?

    1 answer 1

    Assign your controller a delegate for webView.

    Objective-c:

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

    }

    On Swift, something like this, perhaps:

     func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { if(!(request.URL!.isEqual((NSURL(string: url)!)))){ //Если это url не дефолтный (с которого мы запускаем webView), //то открыть через safari UIApplication.sharedApplication().openURL(request.URL!); return false; }else{ //Открыть страницу в webView return true } } 

    UPD:

      let url = "https://yandex.ru/" func webView(sender: WebView!, didStartProvisionalLoadForFrame frame: WebFrame!){ if(!(webView.mainFrameURL.isEqual(url))){ NSWorkspace.sharedWorkspace().openURL(NSURL(string:webView.mainFrameURL)!) } } 
    • Did not pay attention that OS X, added the answer. - Nerevarys
    • Thank you so much for the answer, but Xcode swears at: if (! (WebView.mainFrameURL.isEqual (url))) {on the url variable - Hulwer
    • @Hulwer added variable declaration, try it - Nerevarys
    • Now the browser opens all requests that go inside the WebView which are not strictly equal yandex.ru - Hulwer
    • And I need it at the time of a click) This delegate processes all requests) And I only have those at the click) - Hulwer