SFSafariViewController loading blank white page

i0S Swift Issue

Question or problem in the Swift programming language:

I am using SFSafariViewController to open a URL in my iOS app.. it was working perfectly on iOS 9 but after updating my device to iOS 10, it just loads a blank white page with no URL in the address bar. Even safariViewController(controller: SFSafariViewController, didCompleteInitialLoad didLoadSuccessfully: Bool) is not getting called after controller is presented.

I have imported this in the view controller:

import SafariServices

code:

let url = NSURL(string: urlString)!
if #available(iOS 9.0, *) {
    let safariVC = SFSafariViewController(URL: url)
    safariVC.delegate = self
    self.presentViewController(safariVC, animated: true, completion: {
        self.hideHUD()
     })
} else {
    // Fallback code
}

here is the link to exact same problem someone else faced

How to solve the problem:

Solution 1:

Came across this as seeing some similar problems that I was seeing when trying to use a SafariViewController. Tracked my problems down to using SafariViewController when a popup was present. Have put together some code to show my problem and possible solution.

enter image description here

ViewController.swift:

 /*
 This view controller tries to boil down to the essence of a problem seen 
 when trying to use SafariViewController.  The net result is DO NOT present 
 the SafariViewController when a popup is present.

 This controller and the associated popup show 3 ways to 
 present the SafariViewController:

 Always Good:  This uses a button on the controller to simply call the
 showSVC() routine and never had a problem.

 Good:  This is a work-around for the "Bad" case to follow.  In this case,
 we are using a button in a popup to bring up the SafariViewController.  
 The trick is to get rid of the popup before calling showSVC().  This is 
 done by dismissing the popup immediately without animation and then 
 adding a delay before calling showSVC(). Seems to work fine, but using 
 delays to accomplish things always seems a bit suspect.  Use at your own risk.

 Bad:   When this goes bad, a blank white screen is presented with no way
 to escape.  Using "Reset Content and Settings..." in the simulator can 
 get one back to where it will work one time.  It seemed originally that
 this worked on iOS9 but not on iOS10.  But now seems to fail similarly 
 on both iOS9 and iOS10.  This case is dismissing the popup with animation 
 while trying to bring up the SafariViewController.

 Other info:  This view controller is embedded in a navigation controller - 
 basically to provide a navigation bar and button to act as the anchor 
 point for the popup.  The popup provides two buttons that call back 
 to this controller with the "PopButtonPressedProtocol".  This controller
 uses the title in the popup's buttons to differentiate the "good" and 
 "bad" cases.

 The problems shown by this example sounded similar to problems various 
 others reported, but these reports may be for other reasons, which may 
 or may not be similar.
 */

import UIKit
import SafariServices

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate, PopButtonPressedProtocol {

@IBOutlet weak var btnAlwaysGood: UIButton!
@IBOutlet weak var btnPopup: UIBarButtonItem!

let webAddr = "http://www.google.com"

func delay(_ delay: Double, closure:@escaping ()->()) {
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: closure)
}

func showSVC() {
    let svc = SFSafariViewController(url: URL(string: webAddr)!)
    self.present(svc, animated: true, completion: nil)
}

func popButtonPressed(_ button: UIButton) {
    if let title = button.currentTitle {
        switch title {
        case "Bad":
            dismiss(animated: true, completion: nil)
            showSVC()
        case "Good":
            dismiss(animated: false, completion: nil)
            delay(0.5, closure: { self.showSVC() })
        default:
            break
        }
    }
}

func popupButtonPressed() {
    performSegue(withIdentifier: "popup", sender: nil)
}

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle { // Needed for popup
    return .none
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    btnAlwaysGood.addTarget(self,      action: #selector(showSVC),        for: .touchUpInside)
    btnPopup.target = self
    btnPopup.action = #selector(popupButtonPressed)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let vc = segue.destination as? PopVC {
        vc.delegate = self
        vc.modalPresentationStyle = .popover
        vc.preferredContentSize = CGSize(width: 60, height: 100)
        if let popover = vc.popoverPresentationController {
            popover.permittedArrowDirections = .any
            popover.delegate = self
            popover.barButtonItem = navigationItem.rightBarButtonItem
        }
    }
}
}

PopVC.swift:

import UIKit

protocol PopButtonPressedProtocol : class {
func popButtonPressed(_ button: UIButton)  // protocol:
}

class PopVC: UIViewController {

@IBOutlet weak var btnBad: UIButton!
@IBOutlet weak var btnGood: UIButton!

weak var delegate : PopButtonPressedProtocol?

func buttonPressed(_ button: UIButton) {
    delegate?.popButtonPressed(button)
}

override func viewDidLoad() {
    super.viewDidLoad()
    btnBad.addTarget(self,  action: #selector(buttonPressed(_:)), for: .touchUpInside)
    btnGood.addTarget(self, action: #selector(buttonPressed(_:)), for: .touchUpInside)
}
}

Solution 2:

Try commenting out any loading animations you are doing and try without that.

...
    // self.showHUD()
    //  self.hideHUD()
...

Its because of a bug in iOS 10 introduced during a security fix which doesn’t let safariViewController load when using progress bars and loaders added as a window above the main window

I really wish Apple had documented the change.
I had to comment out the loading indicator in my current app to get it working.

Solution 3:

Swift 5.2, Xcode 11.4

I had similar problem, what I was doing is I was presenting UIAlertViewController and on action of its button I was present SFSafariController….this code worked perfectly on iOS 13 but iOS 12 gave problem…. apparently I had to dismiss the UIAlertViewController first before presenting SFSafariController. Hope this helps.
or else quick fix would be to use UIApplication.shared.openURL()