SFSafariViewController 加载空白页

Posted

技术标签:

【中文标题】SFSafariViewController 加载空白页【英文标题】:SFSafariViewController loading blank white page 【发布时间】:2016-09-23 13:51:08 【问题描述】:

我正在使用 SFSafariViewController 在我的 ios 应用程序中打开一个 URL。它在 iOS 9 上运行良好,但在将我的设备更新到 iOS 10 后,它只是加载一个空白的白页,地址栏中没有 URL。即使 safariViewController(controller: SFSafariViewController, didCompleteInitialLoad didLoadSuccessfully: Bool) 在控制器出现后也不会被调用。

我已经在视图控制器中导入了这个:

import SafariServices

代码:

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 是指向其他人面临的完全相同问题的链接

【问题讨论】:

iOS 10 引入了一些更改,包括内容拦截器。最好验证以下内容。 1. 确保 Safari 内容可见。关闭动画可能会有所帮助 2. 在此处查看 Apple 文档以获取最新更改 ***.com/questions/39019352/… 3. 查看此链接以获取更多建议 ***.com/questions/39019352/… 我已经尝试进行所有更改...似乎没有什么对我有用...奇怪的是其他应用程序的 SFSafariViewController 似乎即使没有更新 ios10 也可以正常工作...跨度> 【参考方案1】:

尝试注释掉您正在执行的任何加载动画,然后尝试不添加。

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

这是因为 iOS 10 在安全修复过程中引入了一个错误,当使用进度条和加载器时,SafariViewController 无法加载,并在主 window 上方添加为 window

我真希望 Apple 已经记录了这一变化。 我必须注释掉当前应用中的加载指示器才能使其正常工作。

【讨论】:

很高兴知道!您可以为调用 VC 的 SFVC 替换为基于子视图的进度/加载指示器。【参考方案2】:

在尝试使用 SafariViewController 时遇到了一些类似的问题。当出现弹出窗口时,将我的问题跟踪到使用 SafariViewController。整理了一些代码来展示我的问题和可能的解决方案。

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)


【讨论】:

【参考方案3】:

Swift 5.2、Xcode 11.4

我遇到了类似的问题,我正在做的是我正在展示 UIAlertViewController 并且在其按钮的操作上我在场 SFSafariController....此代码在 iOS 13 上完美运行,但 iOS 12 出现了问题...。显然我有在呈现 SFSafariController 之前首先关闭 UIAlertViewController。希望这可以帮助。 否则快速解决方法是使用 UIApplication.shared.openURL()

【讨论】:

以上是关于SFSafariViewController 加载空白页的主要内容,如果未能解决你的问题,请参考以下文章

iOS 9 - 在通过 SFSafariViewController 加载的网页底部隐藏工具栏

SFSafariViewController:隐藏导航栏

在 SFSafariViewController 上打开 WebView 链接

新手我可以使用我自己的 SFSafariViewController 并预加载一个 url。然后在故事板上使用它

SFSafariViewController 关闭后的背景音频

如何正确删除 SFSafariViewController 作为子视图控制器?