Bonjour à tous,
Je cherche depuis plusieurs heures et je ne trouve pas de solutions.
Dans mon ViewController de base (storyboard), j’ai fait une fonction avec un UIAlertController avec 2 boutons (addAction) pour ouvrir 2 pages internet.
Or quand je clique dessus sur mon iPhone, ça ne s’ouvre pas.
Je me dit qu’il manque certainement quelque chose. Et notamment l’endroit ou afficher la page.
Pouvez-vous m’aider ?
Voici le code concerné :
func displayAlert () {
let alertController = UIAlertController(title: "Sites Web", message: "Faites votre choix", preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction( title: "Apple",
style: .default,
handler: {
(action:UIAlertAction!) -> Void in
UIApplication.shared.open(URL(fileURLWithPath: "https://www.apple.com/fr/"))
}))
alertController.addAction(UIAlertAction(title: "GitHub",
style: .default,
handler: { (_ ) in
UIApplication.shared.open(URL(fileURLWithPath: "https://github.com/"))
}))
alertController.addAction(UIAlertAction(title: "Fermer", style: .cancel, handler: nil))
present(alertController, animated: true, completion: nil)
}
Il te manque bien ou l’affiche je pense essaye ça
```
func displayAlert (presentedViewController : UIViewController!) {
let alertController = UIAlertController(title: "Sites Web", message: "Faites votre choix", preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction( title: "Apple",
style: .default,
handler: {
(action:UIAlertAction!) -> Void in
UIApplication.shared.open(URL(fileURLWithPath: "https://www.apple.com/fr/"))
}))
alertController.addAction(UIAlertAction(title: "GitHub",
style: .default,
handler: { (_ ) in
UIApplication.shared.open(URL(fileURLWithPath: "https://github.com/"))
}))
alertController.addAction(UIAlertAction(title: "Fermer", style: .cancel, handler: nil))
presentedViewController.present(alertController, animated: true, completion: nil)
}
presentedViewController est le UIViewController dans lequel tu veux l’afficher surement self
Merci Damien pour l’aide mais ça ne fonctionne pas. non plus avec presentingViewController?.present(alertController, animated: true, completion: nil)
ni avec self devant.
en fait dès que je met presentedViewController devant le .present je n’ai plus d’AlertVC qui s’affiche.
Je pense que ça doit se passer dans la completion. il faut je pense remplacer nil par ???
Alors je ne sais pas quand tu souhaite l’appeller ta fonction mais dans ton UIViewController teste ça avec la fonction que je t’ai donnée
override func viewDidAppear(_ animated: Bool) {
displayAlert (presentedViewController : self)
}
func displayAlert (presentedViewController : UIViewController!) {
let alertController = UIAlertController(title: "Sites Web", message: "Faites votre choix", preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction( title: "Apple",
style: .default,
handler: {
(action:UIAlertAction!) -> Void in
UIApplication.shared.open(URL(fileURLWithPath: "https://www.apple.com/fr/"))
}))
alertController.addAction(UIAlertAction(title: "GitHub",
style: .default,
handler: { (_ ) in
UIApplication.shared.open(URL(fileURLWithPath: "https://github.com/"))
}))
alertController.addAction(UIAlertAction(title: "Fermer", style: .cancel, handler: nil))
presentedViewController.present(alertController, animated: true, completion: nil)
}
ça devrait l’afficher quand la vue et apparue
Je viens de tester le code est ça marche par contre pas de redirection gitub ou apple pour moi
Merci Damien pour ton retour.
J’ai trouvé la solution.
Pour que ça fonctionne, il faut utiliser :
UIApplication.shared.open(URL(string: “https://www.apple.com/fr/”)! as URL, options: [:], completionHandler: nil)
et non pas file UIApplication.shared.open(URL(fileURLWithPath: “https://www.apple.com/fr/”))