0

Mi pregunta es sobre swift, SFSafariViewController: Quiero crear una app que al abrirse cargue directamente una web, uso xcode 11.6 pero me da error el archivo sceneDelegate. Si lo suprimo el proyecto no da error pero no se carga ninguna web, este es el error:

generic class UIHostingController requires that vistaSafari conform to view sceneDelegate.

Código:

import UIKIt
import SafariServices

class vistaSafari: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: bool){
cargarWeb()
}
func cargarWeb() {
if let url = URL(string: "https://google.com") {
let safariVC = SFSafariViewController(url: url)
present(safariVC, animated: true, completion: nil)
}
}
}
También he probado:
import UIKIT
import SafariServices

class vistaSafari: UIViewController, SFSafariViewDelegate {

override func viewWillAppear(_ animated: bool) {
let safariVC =SFSafariViewController(url: URL(string: "https://www.google.com")!)
safariVC.delegate = self

present(safariVC, animated: true) } }

4
  • Estás mezclando Swift UI con Storyboard? Por qué no utilizas uno de los dos?
    – Bicho
    el 28 jul. 2020 a las 17:16
  • @Bicho, Trato de hacerlo con swift, en el proyecto elijo swift como lenguaje y swiftUI cono interfaz, no obstante, mis conocimientos no son muy avanzados. He añadido otro código que si bien no da error (salvo con el problema del archivo sceneDelegate), tampoco se me carga la web al iniciar la app.
    – Ender08
    el 29 jul. 2020 a las 13:37
  • Es necesario que lo hagas con Swift UI? Al momento de crear el proyecto puedes seleccionar Storyboards. Intenta creando un nuevo proyecto de tipo Swift/Storyboards.
    – Bicho
    el 29 jul. 2020 a las 14:46
  • Gracias, efectivamente ya he resuelto el error del archivo sceneDelegate cambiando a storyboard, ahora no tengo error en el código pero al ejecutar el proyecto no se me carga la web, debe ser algo muy sencillo pero no doy con la tecla.
    – Ender08
    el 31 jul. 2020 a las 10:34

1 respuesta 1

0

Si no te muestra y si tienes algún warning en la consola, debes agregar el código en viewDidAppear(), si lo agregas en viewDidLoad() no funciona porque no puede mostrar el ViewController sin cargarlo primero.

Te dejo el código y no te olvides de agregar el protocolo del delegate:

import UIKit
import SafariServices//Importas el framework. 

class ViewController: UIViewController, SFSafariViewControllerDelegate /*Protocolo del delegate*/ {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
    }
    //Aquí agregas el código.
    override func viewDidAppear(_ animated: Bool) {
        let urlString = "https://www.google.com.ec"//La url.
        
        if let url = URL(string: urlString) {
            let safariVC = SFSafariViewController(url: url)//Instancias SFSafariViewController.
            safariVC.delegate = self//Agregas el delegado.
            
            present(safariVC, animated: true, completion: nil)//Muestras el VC.
        }
    }
}

Resultado

introducir la descripción de la imagen aquí

2
  • el código no arroja ningún error pero al correr la app la sigue sin cargar la web, no lo entiendo, parece estar todo correcto.
    – Ender08
    el 2 ago. 2020 a las 17:22
  • Puedes intentar creando un nuevo proyecto sin eliminar ningún archivo ni modificando el .plist. Yo lo hice así y funciona.
    – Bicho
    el 2 ago. 2020 a las 19:58

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.