You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.4 KiB

6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <TargetConditionals.h>
  17. #if !TARGET_OS_OSX && !TARGET_OS_TV
  18. #import "FIRAuthWebViewController.h"
  19. #import "FIRAuthWebView.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. @interface FIRAuthWebViewController () <WKNavigationDelegate>
  22. @end
  23. @implementation FIRAuthWebViewController {
  24. /** @var _URL
  25. @brief The initial URL to display.
  26. */
  27. NSURL *_URL;
  28. /** @var _delegate
  29. @brief The delegate to call.
  30. */
  31. __weak id<FIRAuthWebViewControllerDelegate> _delegate;
  32. /** @var _webView;
  33. @brief The web view instance for easier access.
  34. */
  35. __weak FIRAuthWebView *_webView;
  36. }
  37. - (instancetype)initWithURL:(NSURL *)URL
  38. delegate:(__weak id<FIRAuthWebViewControllerDelegate>)delegate {
  39. self = [super initWithNibName:nil bundle:nil];
  40. if (self) {
  41. _URL = URL;
  42. _delegate = delegate;
  43. }
  44. return self;
  45. }
  46. #pragma mark - Lifecycle
  47. - (void)loadView {
  48. FIRAuthWebView *webView = [[FIRAuthWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  49. webView.webView.navigationDelegate = self;
  50. self.view = webView;
  51. _webView = webView;
  52. self.navigationItem.leftBarButtonItem =
  53. [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
  54. target:self
  55. action:@selector(cancel)];
  56. }
  57. - (void)viewDidAppear:(BOOL)animated {
  58. [super viewDidAppear:animated];
  59. // Loads the requested URL in the web view.
  60. [_webView.webView loadRequest:[NSURLRequest requestWithURL:_URL]];
  61. }
  62. #pragma mark - UI Targets
  63. - (void)cancel {
  64. [_delegate webViewControllerDidCancel:self];
  65. }
  66. #pragma mark - WKNavigationDelegate
  67. - (void)webView:(WKWebView *)webView
  68. decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
  69. decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  70. [_delegate webViewController:self canHandleURL:navigationAction.request.URL];
  71. decisionHandler(WKNavigationActionPolicyAllow);
  72. }
  73. - (void)webView:(WKWebView *)webView
  74. didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation {
  75. _webView.spinner.hidden = NO;
  76. [_webView.spinner startAnimating];
  77. }
  78. - (void)webView:(WKWebView *)webView
  79. didFinishNavigation:(null_unspecified WKNavigation *)navigation {
  80. _webView.spinner.hidden = YES;
  81. [_webView.spinner stopAnimating];
  82. }
  83. - (void)webView:(WKWebView *)webView
  84. didFailNavigation:(null_unspecified WKNavigation *)navigation
  85. withError:(NSError *)error {
  86. if ([error.domain isEqualToString:NSURLErrorDomain] && error.code == NSURLErrorCancelled) {
  87. // It's okay for the page to be redirected before it is completely loaded. See b/32028062 .
  88. return;
  89. }
  90. // Forward notification to our delegate.
  91. [self webView:webView didFinishNavigation:navigation];
  92. [_delegate webViewController:self didFailWithError:error];
  93. }
  94. @end
  95. NS_ASSUME_NONNULL_END
  96. #endif