LoginSignup
0
1

More than 1 year has passed since last update.

SFSafariViewController を SwiftUI でいい感じに使う

Last updated at Posted at 2022-02-04

はじめに

Webページを表示したい(例えばお問い合わせフォーム)時にSFSafariViewControllerを使うと思いますが、SwiftUIでは一つのViewとして扱うことが出来ます。
UIをオーバーレイしたいとかで、WKWebViewを頑張ってカスタマイズせずに利用出来ます。

実装

import SwiftUI
import SafariServices

struct SafariView: UIViewControllerRepresentable {
    var url: URL
    typealias UIViewControllerType = SFSafariViewController

    func makeUIViewController(context: Context) -> SFSafariViewController {
        let config = SFSafariViewController.Configuration()
        config.barCollapsingEnabled = false
        let safariViewController = SFSafariViewController(url: url, configuration: config)
        return safariViewController
    }

    func updateUIViewController(_ uiViewController: SFSafariViewController, context: Context) {
    }
}

struct SafariView_Previews: PreviewProvider {
    static var previews: some View {
        SafariView(url: URL(string: "")!)
    }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1