iOS14开发

iOS开发之SFSafariViewController

2023-02-03  本文已影响0人  YungFan

介绍

案例

import SafariServices
import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        showSafariViewController()
    }

    // MARK: 显示SFSafariViewController
    func showSafariViewController() {
        // URL
        let url = URL(string: "https://www.baidu.com")
        // 创建SFSafariViewController
        let safariViewController = SFSafariViewController(url: url!)
        // 设置代理
        safariViewController.delegate = self
        // 显示
        present(safariViewController, animated: true, completion: nil)
    }
}

extension ViewController: SFSafariViewControllerDelegate {
    // MARK: 点击左上角的完成
    func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
        print(#function)
    }

    // MARK: 加载完成
    func safariViewController(_ controller: SFSafariViewController, didCompleteInitialLoad didLoadSuccessfully: Bool) {
        print(#function)
    }
    
    // MARK: 点击右下角调用Safari浏览器打开
    func safariViewControllerWillOpenInBrowser(_ controller: SFSafariViewController) {
        print(#function)
    }
}

注意:SFSafariViewController 只能加载 HTTP/HTTPS 的 URL。

上一篇下一篇

猜你喜欢

热点阅读