3

I'm using the following code for opening an URL with SFSafariViewController:

import UIKit
import SafariServices

class ViewController: UIViewController, UITextViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        openBrowser(url: "https://www.example.com")
    }
    fileprivate func openBrowser(url: String) {
        let svc = SFSafariViewController(url: NSURL(string: url)! as URL, entersReaderIfAvailable: false)
        svc.delegate = self
        present(svc, animated: true, completion: nil)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

extension ViewController: SFSafariViewControllerDelegate {
    func safariViewController(_ controller: SFSafariViewController, didCompleteInitialLoad didLoadSuccessfully: Bool) {
    }
    func safariViewController(_ controller: SFSafariViewController, activityItemsFor URL: URL, title: String?) -> [UIActivity] {
        return []
    }
    func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
    }
}

This is working very well.

But if the website contains a link with target="_blank" and I'm clicking on it nothing happens.

I know that SFSafariViewController doesn't support multiple windows or tabs.

But I think there should be a possibility to ignore target="_blank" and open the link like there is no target="_blank".

Is there any way to reach this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
David
  • 2,898
  • 3
  • 21
  • 57
  • You need to implement that yourself. Meaning you need to set yourself as SFSafariViewControllerDelegate and control each `itemClick` action. – Lifely Dec 10 '18 at 09:28
  • @Lifely Thanks, I'll try it. Is there a way to edit the loaded HTML code? If yes, I could search remove all `target="_blank"`. – David Dec 10 '18 at 09:38
  • Yes you can do it, I think you'll need to use a script in javascript though – Lifely Dec 10 '18 at 10:28
  • Does this happen for every website that you open? I have just tried the same thing and `target="_blank"` links just open in the same window, as you want to happen. – darrenallen7 Dec 10 '18 at 10:31
  • 1
    @darrenallen7 That is a strange thing: Sometimes its's working an sometimes it's not working. Maybe it's not the `target="_blank"`? I'll make some tests and get back to you. – David Dec 10 '18 at 10:33
  • 1
    Are you able to send the link that you are opening? I can try that one too. – darrenallen7 Dec 10 '18 at 10:37
  • 1
    @darrenallen7 I found out something: I can only click one single `target="_blank"` link. When clicking another `target="_blank"` link on this page (I opened by `target="_blank"`) this won't work. But I can use the back button and click another (or the same) `target="_blank"` link. this will work. – David Dec 10 '18 at 13:27
  • 1
    @darrenallen7 Maybe better understandable: Open the app and launch an URL inside `SFSafariViewControllerDelegate`. Click on a `target="_blank"` link (will work). Click on a target `target="_blank"` link on the newly opened webpage (won't work). – David Dec 10 '18 at 13:30
  • @darrenallen7 It seems like every `target="_blank"` link opens a child window that doesn't allow `target="_blank"` links itself. Do you understand what I mean? – David Dec 10 '18 at 13:31
  • @David yep I understand, I can confirm I have the same behaviour. I have not come across this before. – darrenallen7 Dec 10 '18 at 13:34
  • @darrenallen7 Do you have any idea how to solve this? – David Dec 10 '18 at 14:16
  • I can't see a solution at the moment. Will investigate a bit more. – darrenallen7 Dec 10 '18 at 15:09
  • @darrenallen7 Thank you very much! – David Dec 10 '18 at 18:20
  • 1
    Hello guys, did you find any solution? thanks! – jdnichollsc Apr 17 '19 at 04:09

0 Answers0