如何在应用内浏览器中打开我的iOS应用中单击的任何URL?

时间:2018-10-16 03:34:36

标签: ios swift inappbrowser sfsafariviewcontroller

如何设置规则,以便当用户单击应用程序中的任何Web链接时,该链接在应用程序内浏览器而不是Safari中打开?

上下文 :我正在构建一个应用,该应用具有多个位置,这些位置可以由我嵌入或通过各种用户交互(例如,指向指向包含另一个链接的页面的链接。我想确保用户很少离开我的应用程序,因此想在已经开发的应用程序内浏览器中打开所有外部Web链接

目标版本:iOS 11。 环境/语言:Swift 4

2 个答案:

答案 0 :(得分:2)

  

...它是在应用内浏览器而非Safari中打开的吗?

在这种情况下,您将需要自己的WebView。这是控制器内的简单webView的快速片段:

import UIKit
import WebKit

/// The controller for handling webviews.
class WebViewController: UIViewController {

    // MARK: - Properties
    internal lazy var button_Close: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(.close, for: .normal)
        button.imageEdgeInsets = UIEdgeInsets.init(top: 0, left: -30, bottom: 0, right: 0)
        button.addTarget(self, action: #selector(back(_:)), for: .touchUpInside)
        return button
    }()

    public var urlString: String! {
        didSet {
            if let url = URL(string: urlString) {
                let urlRequest = URLRequest(url:url)
                self.webView.load(urlRequest)
            }
        }
    }

    private lazy var webView: WKWebView = {
        let webView = WKWebView()
        webView.navigationDelegate = self
        return webView
    }()

    // MARK: - Functions
    // MARK: Overrides

    override func viewDidLoad() {
        super.viewDidLoad()

        let barButton = UIBarButtonItem(customView: self.button_Close)
        self.button_Close.frame = CGRect(x: 0, y: 0, width: 55.0, height: 44.0)
        let negativeSpacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: nil, action: nil)
        if #available(iOS 11.0, *) {
            negativeSpacer.width = -30
        }
        self.navigationItem.leftBarButtonItems = [negativeSpacer, barButton]

        self.view.addSubview(self.webView)
        self.webView.snp.makeConstraints {
            $0.edges.equalToSuperview()
        }
    }

    @objc func back(_ sender: Any) {
        self.dismiss()
    }
}

extension WebViewController: WKNavigationDelegate {
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        // Show here a HUD or any loader
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        // Dismiss your HUD
    }
}

并呈现这样的webViewController,就像这样(传递URLString):

let webViewVC = WebViewController()
webViewVC.urlString = "https://www.glennvon.com/"
let navCon = UINavigationController(rootViewController: webViewVC)
self.navigationController?.present(navCon, animated: true, completion: nil)

如果使用情节提要,只需在控制器中拖动Web视图并设置委托即可。这应该可以帮助您:)

答案 1 :(得分:1)

使用SFSafariViewController的简单解决方案,该解决方案在单独的软件包中提供

import SafariServices

Swift 4,iOS 9.0 +

let url = URL(string: "your URL here")
let vc = SFSafariViewController(url: url)
present(vc, animated: true)

如果您需要更多控制权-使用Configuration参数( Swift 4,iOS 11.0 + ):

示例:

let config = SFSafariViewController.Configuration()
config.entersReaderIfAvailable = true

let url = URL(string: "your URL here")
let vc = SFSafariViewController(url: url, configuration: config)
present(vc, animated: true)