No Description

FIRAuthWebViewController.m 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #import "FIRAuthWebViewController.h"
  17. #import "FIRAuthWebView.h"
  18. NS_ASSUME_NONNULL_BEGIN
  19. @interface FIRAuthWebViewController () <WKNavigationDelegate>
  20. @end
  21. @implementation FIRAuthWebViewController {
  22. /** @var _URL
  23. @brief The initial URL to display.
  24. */
  25. NSURL *_URL;
  26. /** @var _delegate
  27. @brief The delegate to call.
  28. */
  29. __weak id<FIRAuthWebViewControllerDelegate> _delegate;
  30. /** @var _webView;
  31. @brief The web view instance for easier access.
  32. */
  33. __weak FIRAuthWebView *_webView;
  34. }
  35. - (instancetype)initWithURL:(NSURL *)URL
  36. delegate:(__weak id<FIRAuthWebViewControllerDelegate>)delegate {
  37. self = [super initWithNibName:nil bundle:nil];
  38. if (self) {
  39. _URL = URL;
  40. _delegate = delegate;
  41. }
  42. return self;
  43. }
  44. #pragma mark - Lifecycle
  45. - (void)loadView {
  46. FIRAuthWebView *webView = [[FIRAuthWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  47. webView.webView.navigationDelegate = self;
  48. self.view = webView;
  49. _webView = webView;
  50. self.navigationItem.leftBarButtonItem =
  51. [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
  52. target:self
  53. action:@selector(cancel)];
  54. }
  55. - (void)viewDidAppear:(BOOL)animated {
  56. [super viewDidAppear:animated];
  57. // Loads the requested URL in the web view.
  58. [_webView.webView loadRequest:[NSURLRequest requestWithURL:_URL]];
  59. }
  60. #pragma mark - UI Targets
  61. - (void)cancel {
  62. [_delegate webViewControllerDidCancel:self];
  63. }
  64. #pragma mark - WKNavigationDelegate
  65. - (void)webView:(WKWebView *)webView
  66. decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
  67. decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  68. [_delegate webViewController:self canHandleURL:navigationAction.request.URL];
  69. decisionHandler(WKNavigationActionPolicyAllow);
  70. }
  71. - (void)webView:(WKWebView *)webView
  72. didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation {
  73. _webView.spinner.hidden = NO;
  74. [_webView.spinner startAnimating];
  75. }
  76. - (void)webView:(WKWebView *)webView
  77. didFinishNavigation:(null_unspecified WKNavigation *)navigation {
  78. _webView.spinner.hidden = YES;
  79. [_webView.spinner stopAnimating];
  80. }
  81. - (void)webView:(WKWebView *)webView
  82. didFailNavigation:(null_unspecified WKNavigation *)navigation
  83. withError:(NSError *)error {
  84. if ([error.domain isEqualToString:NSURLErrorDomain] && error.code == NSURLErrorCancelled) {
  85. // It's okay for the page to be redirected before it is completely loaded. See b/32028062 .
  86. return;
  87. }
  88. // Forward notification to our delegate.
  89. [self webView:webView didFinishNavigation:navigation];
  90. [_delegate webViewController:self didFailWithError:error];
  91. }
  92. @end
  93. NS_ASSUME_NONNULL_END