Files
firebase-ios-sdk/FirebaseAuth/Sources/Swift/Utilities/AuthWebViewController.swift
T

135 lines
4.8 KiB
Swift
Raw Normal View History

2023-03-26 13:43:52 +02:00
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#if os(iOS)
2023-03-26 08:23:34 -07:00
import Foundation
import UIKit
import WebKit
2023-03-26 13:43:52 +02:00
/// Defines a delegate for AuthWebViewController
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
2023-05-08 16:25:58 -07:00
protocol AuthWebViewControllerDelegate: AnyObject {
/// Notifies the delegate that the web view controller is being cancelled by the user.
/// - Parameter webViewController: The web view controller in question.
2023-05-08 16:25:58 -07:00
func webViewControllerDidCancel(_ controller: AuthWebViewController)
2023-03-26 13:43:52 +02:00
/// Determines if a URL should be handled by the delegate.
/// - Parameter url: The URL to handle.
/// - Returns: Whether the URL could be handled or not.
2023-05-08 16:25:58 -07:00
func webViewController(_ controller: AuthWebViewController, canHandle url: URL) -> Bool
/// Notifies the delegate that the web view controller failed to load a page.
/// - Parameter webViewController: The web view controller in question.
/// - Parameter error: The error that has occurred.
2023-05-08 16:25:58 -07:00
func webViewController(_ controller: AuthWebViewController, didFailWithError error: Error)
/// Presents an URL to interact with user.
/// - Parameter url: The URL to present.
/// - Parameter uiDelegate: The UI delegate to present view controller.
/// - Parameter completion: A block to be called either synchronously if the presentation fails
/// to start, or asynchronously in future on an unspecified thread once the presentation
/// finishes.
2023-05-08 16:25:58 -07:00
func present(_ url: URL,
uiDelegate: AuthUIDelegate?,
callbackMatcher: @escaping (URL?) -> Bool,
completion: @escaping (URL?, Error?) -> Void)
}
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
2023-05-08 16:25:58 -07:00
class AuthWebViewController: UIViewController,
2023-03-26 08:23:34 -07:00
WKNavigationDelegate {
// MARK: - Properties
2023-03-26 13:43:52 +02:00
2023-03-26 08:23:34 -07:00
private var url: URL
2023-05-08 16:25:58 -07:00
weak var delegate: AuthWebViewControllerDelegate?
2023-03-26 08:23:34 -07:00
private weak var webView: AuthWebView?
2023-03-26 13:43:52 +02:00
2023-03-26 08:23:34 -07:00
// MARK: - Initialization
2023-03-26 13:43:52 +02:00
2023-05-08 16:25:58 -07:00
init(url: URL, delegate: AuthWebViewControllerDelegate) {
2023-03-26 08:23:34 -07:00
self.url = url
self.delegate = delegate
super.init(nibName: nil, bundle: nil)
}
2023-03-26 13:43:52 +02:00
2023-03-26 08:23:34 -07:00
@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
2023-03-26 13:43:52 +02:00
2023-03-26 08:23:34 -07:00
// MARK: - View Lifecycle
2023-03-26 13:43:52 +02:00
2023-11-13 08:47:17 -08:00
override func loadView() {
2023-03-26 08:23:34 -07:00
let webView = AuthWebView(frame: UIScreen.main.bounds)
webView.webView.navigationDelegate = self
view = webView
self.webView = webView
navigationItem.leftBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .cancel,
target: self,
action: #selector(cancel)
)
}
2023-03-26 13:43:52 +02:00
2023-11-13 08:47:17 -08:00
override func viewDidAppear(_ animated: Bool) {
2023-03-26 08:23:34 -07:00
super.viewDidAppear(animated)
webView?.webView.load(URLRequest(url: url))
}
2023-03-26 13:43:52 +02:00
2023-03-26 08:23:34 -07:00
// MARK: - Actions
2023-03-26 13:43:52 +02:00
2023-03-26 08:23:34 -07:00
@objc private func cancel() {
delegate?.webViewControllerDidCancel(self)
}
2023-03-26 13:43:52 +02:00
// MARK: - WKNavigationDelegate
2023-11-13 08:47:17 -08:00
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
2023-03-26 08:23:34 -07:00
let canHandleURL = delegate?.webViewController(
self,
canHandle: navigationAction.request.url ?? url
) ?? false
if canHandleURL {
decisionHandler(.allow)
} else {
decisionHandler(.cancel)
}
2023-03-26 13:43:52 +02:00
}
2023-11-13 08:47:17 -08:00
func webView(_ webView: WKWebView,
didStartProvisionalNavigation navigation: WKNavigation!) {
2023-03-26 08:23:34 -07:00
self.webView?.spinner.isHidden = false
self.webView?.spinner.startAnimating()
2023-03-26 13:43:52 +02:00
}
2023-11-13 08:47:17 -08:00
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
2023-03-26 08:23:34 -07:00
self.webView?.spinner.isHidden = true
self.webView?.spinner.stopAnimating()
2023-03-26 13:43:52 +02:00
}
2023-11-13 08:47:17 -08:00
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!,
withError error: Error) {
2023-03-26 08:23:34 -07:00
if (error as NSError).domain == NSURLErrorDomain,
(error as NSError).code == NSURLErrorCancelled {
// It's okay for the page to be redirected before it is completely loaded. See b/32028062 .
return
}
// Forward notification to our delegate.
self.webView(webView, didFinish: navigation)
delegate?.webViewController(self, didFailWithError: error)
2023-03-26 13:43:52 +02:00
}
2023-03-26 08:23:34 -07:00
}
2023-03-26 13:43:52 +02:00
#endif