Swift 4.2
Here's how you can open the Safari browser in your application.
import SafariServices
whenever you want to open how wise on
@IBAction func btnOpenWebTapped(_ sender: UIButton) { self.openWeb(contentLink: "https://www.google.com") } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { self.openWeb(contentLink: "https://www.google.com") } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.openWeb(contentLink: "https://www.google.com") }
write a custom function like you, and you can set the properties of SFSafariViewController, preferredBarTintColor, preferredControlTintColor, dismissButtonStyle, barCollapsingEnabled
func openWeb(contentLink : String){ let url = URL(string: contentLink)! let controller = SFSafariViewController(url: url) controller.preferredBarTintColor = UIColor.darkGray controller.preferredControlTintColor = UIColor.groupTableViewBackground controller.dismissButtonStyle = .close controller.configuration.barCollapsingEnabled = true self.present(controller, animated: true, completion: nil) controller.delegate = self }
Last and most important: Remember to associate the SFSafariViewController delegate with your view controller. You can do this with the extension code below.
extension YourViewController: SFSafariViewControllerDelegate { func safariViewControllerDidFinish(_ controller: SFSafariViewController) { controller.dismiss(animated: true, completion: nil) } }
Good coding Thank you :)
source share