0

I have a custom UITabBarController with custom button. When I click this button I open SFSafariViewController - here all works fine. But when I clicked to "done" button in SFSafariViewController its dismiss. but I can't return to UITabBarController, I see only background color which I added in app delegate window.

Sample code:

class TabBar: UITabBarController, UITabBarControllerDelegate, SFSafariViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
    }

    func setupCenterButton() {
        let centerButton = CenterButton(frame: CGRect(x: 0, y: 0, width: 74, height: 74))
        centerButton.layer.cornerRadius = 37
        centerButton.frame.origin.y = self.tabBar.frame.minY - 37
        centerButton.frame.origin.x = view.bounds.width/2 - 37
        centerButton.setImage(UIImage(named: "google"), for: .normal)
        centerButton.addTarget(self, action: #selector(openURL), for: .touchUpInside)
        view.addSubview(centerButton)
        view.layoutIfNeeded()
    }

    @objc func openURL() {
        let termsURL = SFSafariViewController(url: URL(string: "https://google.ru")!)
        termsURL.modalPresentationStyle = .currentContext
        termsURL.delegate = self
        self.present(termsURL, animated: true, completion: nil)
    }
}

How can I present UITabBarController after SFSafariViewController?

Mayur Karmur
  • 2,119
  • 14
  • 35
intacto
  • 15
  • 4
  • You need to implement the `safariViewControllerDidFinish` delegate method and actually dismiss the safari view controller; you tab bar controller is hidden by the "empty" safari view controller. – Paulw11 Jun 10 '19 at 13:07
  • what should I do in this method? controller.dismiss(animated: true, completion: nil) doesn't work – intacto Jun 10 '19 at 13:42
  • Why do you want to call this in the TabBarController? If it is your intention to add this button to the tabor, then the answers may help you out, but otherwise, I would setup a Viewcontroller embedded in the TabBarController and create this button and SFSafariViewController in the ViewController. – Christopher Haas Sep 29 '21 at 20:43
  • To add on, I found that changing the SafariVC's `.modalPresentationStyle = .overCurrentContext` achieved what I was looking for and might help you, but I did not test calling from TabBarController. – Christopher Haas Sep 29 '21 at 20:56

2 Answers2

0
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
    //To access the  Specific tabBar ViewController
    let tabBarController = storyboard?.instantiateViewController(withIdentifier: "IdentifierTabbar") as! TabBar
   // tabBarController.isComingFrom = "Settings" // Assign the Value                
    window?.rootViewController = tabBarController
}
0

I had same problem, I solved selecting another tab bar item programmatically when I tap "Done" on my SafariViewController. In my exemple I select first item of my tab bar.

import UIKit
import SafariServices
    
class WebViewController: UIViewController, SFSafariViewControllerDelegate {
        
    override func loadView() {

    }
    override func viewDidLoad() {
        super.viewDidLoad()
        guard let url = URL(string: "<your website url>") else {
            return
        }

        let safariVC = SFSafariViewController(url: url)
        present(safariVC, animated: true, completion: {self.selectFirstTab()})
        
    }
    
    func selectFirstTab(){
        self.tabBarController?.selectedIndex = 0
    }

}